通过Click事件中的c#文件重置样式值

问题描述 投票:1回答:2

我有一个在Xaml中声明的按钮样式,我在我的C#文件中执行了一个Click事件,当点击时,更改了一些样式但是现在,当再次单击时,我想将我的样式重置为inicial样式。我如何管理点击并重置样式?

<flv:FlowListView FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="true"
                    FlowItemTappedCommand="{Binding ItemTappedCommand}" FlowLastTappedItem="{Binding LastTappedItem}"
                    FlowItemsSource="{Binding MyCategories}" >

    <flv:FlowListView.FlowColumnTemplate>
      <DataTemplate>
         <Button Text="{Binding Name}"
         TextColor="White"
         x:Name="categoryButtons"
         Clicked="ButtonSelected"
         ContentLayout="Top"
         BackgroundColor="Transparent"
         BorderColor="White"
         BorderWidth="2"
         CornerRadius="6"
         Margin="5,5,5,10" />
      </DataTemplate>
   </flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>
public void ButtonSelected(object sender, EventArgs e)
        {
            var button = (Button)sender;
            button.BackgroundColor = Color.Blue;
        }
c# xaml xamarin xamarin.forms
2个回答
0
投票

好的,所以你的Flow ItemsSource是你的qazxsw poi或你正在使用的任何模型对象类型的qazxsw poi。 DataTemplate中的按钮绑定到Binded类的Name属性。

按如下方式为按钮分配触发器,然后单击该按钮时,根据当前状态将模型对象中的属性设置为true / false。

ObservableCollection<Category>

另外,如上所示,我建议在您的Category类中放置一个Category属性来执行bool属性的更改。

Category模型对象的Command / Code看起来像这样:

<Button
    Text="{Binding Name}"
    TextColor="White"
    Command="{Binding ToggleCommand}"
    ContentLayout="Top"
    BackgroundColor="Transparent"
    BorderColor="White"
    BorderWidth="2"
    CornerRadius="6"
    Margin="5,5,5,10" >
    <Button.Triggers>
        <DataTrigger
            TargetType="Button"
            Binding="{Binding IsToggled}"
            Value="True">
            <Setter Property="BackgroundColor" Value="Blue" />
        </DataTrigger>
    </Button.Triggers>
</Button>

然后,一旦您的状态切换为true,按钮将变为蓝色,然后当它为false时,它将返回到原始透明状态。

另外,请确保您的Category类正在实现ICommand


0
投票

所以我这样做的样式重置为默认值:

//Constructor
public Category()
{
    ToggleCommand = new Command(() =>
    {
        IsToggled = !IsToggled;
        NotifyPropertyChanged(nameof(IsToggled));
    };
}

public ICommand ToggleCommand { get; }

public bool IsToggled { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.