如何更新CS文件中按钮的样式?

时间:2019-04-29 15:17:16

标签: c# xaml xamarin.forms

我正在尝试更新用xaml代码创建的cs文件(c#)中的按钮样式。

我搜索了很多解决方案,但都没有用。

<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)
        {

        }

我有这个
regular icon
我要这个
highlighted icon
忽略两个图标之间的区别

1 个答案:

答案 0 :(得分:0)

根据您的描述,为Button创建样式,然后要在单击Button时更改某些Button属性,建议您直接更改Button属性,如下所示:

 <StackLayout>
    <Label
        HorizontalOptions="Center"
        Text="Welcome to Xamarin.Forms!"
        VerticalOptions="CenterAndExpand" />

    <flv:FlowListView
        FlowColumnCount="3"
        FlowItemsSource="{Binding categories}"
        HasUnevenRows="True"
        SeparatorVisibility="None">
        <flv:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <Button
                    x:Name="CategoryButtons"
                    Margin="5,5,5,10"
                    Clicked="CategoryButtons_Clicked"
                    Style="{DynamicResource buttonstyle}"
                    Text="{Binding Name}" />
            </DataTemplate>
        </flv:FlowListView.FlowColumnTemplate>
    </flv:FlowListView>

    <Button
        x:Name="btn1"
        BackgroundColor="Transparent"
        BorderColor="White"
        BorderWidth="2"
        Clicked="btn1_Clicked"
        HeightRequest="40"
        Text="this is test!"
        WidthRequest="300" />
</StackLayout>

  <Application.Resources>
    <ResourceDictionary>
        <Style x:Key="buttonstyle" TargetType="Button">
            <Setter Property="BackgroundColor" Value="Transparent" />
            <Setter Property="BorderWidth" Value="2" />
            <Setter Property="CornerRadius" Value="6" />
            <Setter Property="ContentLayout" Value="Top" />
            <Setter Property="TextColor" Value="White" />
            <Setter Property="BorderColor" Value="White" />

        </Style>
    </ResourceDictionary>
</Application.Resources>


  private void CategoryButtons_Clicked(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        btn.BorderColor = Color.Orange;



    }