wpf 如何以编程方式设置样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3199424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:55:06 来源:igfitidea点击:
How to set the style programmatically
提问by user384080
I have the following style but i need to make it programmatically:
我有以下风格,但我需要以编程方式制作:
<xcdg:DataGridControl MinHeight="300"
Name="listViewUnallocated"
ItemsSource="{Binding Source={StaticResource
cvs_unallocatedTerminals}}"
AllowDrop="True"
Drop="Grid_Drop"
MouseMove="Grid_MouseMove"
KeyUp="listViewUnallocated_KeyUp"
MouseDoubleClick="gridUnallocated_MouseDoubleClick"
ReadOnly="True"
DockPanel.Dock="Top">
<xcdg:DataGridControl.Resources>
<Style TargetType="{x:Type xcdg:DataRow}" x:Name="selectedStyleTrigger">
<Style.Triggers>
<DataTrigger Binding="{Binding TerminalId}" Value="72948028">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</xcdg:DataGridControl.Resources>
回答by Grant Thomas
In the code-behind file of the control, try:
在控件的代码隐藏文件中,尝试:
this.Style = Resources["ResourceName"] as Style;
回答by Mangesh
Set x:Key
in XAML & in code-behind use:
设置x:Key
在XAML和代码隐藏使用:
something.Style = (Style) FindResource("YourResourceKey");
回答by Vishwash Roy
Hi we can set style programmatically like this.
嗨,我们可以像这样以编程方式设置样式。
Style rowStyle = new Style(typeof(DataGridRow));
DataTrigger dataTrigger = new DataTrigger("TerminalId");
Binding binding = new Binding();
dataTrigger.Binding = binding;
dataTrigger.Value = 72948028;
Setter setter = new Setter(DataGridRow.BackgroundProperty, Brushes.Red);
dataTrigger.Setters.Add(setter);
rowStyle.Triggers.Add(dataTrigger);
listViewUnallocated.RowStyle = rowStyle;