【问题标题】:How can I update the style of a button in cs file?如何更新cs文件中按钮的样式?
【发布时间】:2020-08-31 14:01:38
【问题描述】:

我正在尝试更新我在 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)
        {

        }

我有这个

我想要这个

忽略两个图标的区别

【问题讨论】:

  • @LarryTang 我看到了那个解决方案,但它太令人困惑了,可能有更好的解决方案,而不是那个令人困惑的解决方案,这就是我正在寻找的。我希望更新不在我的 c# 文件中创建。
  • 您是否有理由不希望在 Xaml 中声明/使用该样式?
  • @TaylorD 我希望在我的 Xaml 文件中声明我的样式,因为 FlowListView 代码,但如果它更好,我可以在 ResourceDictionary 中删除一些样式。我只是希望能够通过 cs 文件(c#)更改我的按钮样式。 (抱歉耽搁了)

标签: c# xaml xamarin.forms


【解决方案1】:

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

XAML

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

CS

private void CategoryButtons_Clicked(object sender, EventArgs e)
{
    Button btn = (Button)sender;       
    btn.BorderColor = Color.Orange;
   }

【讨论】:

  • 非常感谢,这个解决方案有效,我做了一点不同,没有创建 ResourceDictionary 和其他按钮,我只是保留了我的 Xaml 代码并完成了你发给我的 c# 代码。但是现在,如果你能帮助我,当我再次点击按钮时,我可以重置样式吗?就像,我在按钮中单击一次样式会更改,但是当我再次单击时,我想重置样式。
猜你喜欢
  • 1970-01-01
  • 2019-10-06
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多