如何以编程方式设置样式

时间:2010-07-07 22:33:37

标签: wpf

我有以下样式,但我需要以编程方式进行:

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

3 个答案:

答案 0 :(得分:43)

在控件的代码隐藏文件中,尝试:

this.Style = Resources["ResourceName"] as Style;

答案 1 :(得分:32)

在XAML&amp;中设置x:Key在代码隐藏使用中:

something.Style = (Style) FindResource("YourResourceKey");

答案 2 :(得分:8)

您好我们可以像这样以编程方式设置样式。

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;