如何更新CS文件中按钮的样式? [英] How can I update the style of a button in cs file?
本文介绍了如何更新CS文件中按钮的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更新我在xaml代码中创建的cs文件(c#)中的按钮样式.
Im trying to update a style of a button, in cs file (c#), that I created in xaml code.
我搜索了很多解决方案,但是都没有用.
I searched for a lot solutions but none worked.
<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)
{
}
我有这个
我想要这个
忽略两个图标之间的区别
I have this
I want this
Ignore the difference between the two icons
推荐答案
根据您的描述,为Button创建样式,然后单击Button时要更改某些Button属性,建议您直接更改Button属性,例如这个:
According to your description, you create style for Button, then you want to change some Button property when Button click, I suggest you can change Button property directly, like this:
<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;
}
这篇关于如何更新CS文件中按钮的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文