简体   繁体   中英

WPF setting Style StaticResource in ViewModel

I've a GridControl in WPF (it's from DevExpress but that's not really the point) and I'm trying to style the headers based on a StaticResource .

Normally what I could do is

<UserControl.Resources>
    <Style x:Key="HeaderStyle" TargetType="dxg:HeaderContentControl">
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
</UserControl.Resources>

<dxg:GridControl x:Name="MyParameters" ItemsSource="{Binding ParamRows}">
    <dxg:GridColumn ColumnHeaderContentStyle="{StaticResource HeaderStyle}" x:Name="ParamName" FieldName="ParamName" Width="80" Header="Parameter" />
    <dxg:GridColumn ColumnHeaderContentStyle="{StaticResource HeaderStyle}" x:Name="ParamValue" Binding="{Binding ParamValue}" Width="50"  Header="Value" />
<!-- etc.  -->

..and that would work fine.

However, I'm building the columns dynamically in the ViewModel so I need to be able to set the ColumnHeaderContentStyle programmatically at run-time.

So the XAML has...

<dxg:GridControl x:Name="Parameters" ItemsSource="{Binding ParamRows}" ColumnsSource="{Binding ParamColumns}">
    <!-- no list of rows.  -->

... and in the C# Code...

ParamColumns.Add(new GridColumn
{
    ColumnHeaderContentStyle = (Style)Application.Current.Resources["HeaderStyle"],
    FieldName = "ParamName",
    Width=80,
    Header="Parameter"
});


ParamColumns.Add(new GridColumn
{
    ColumnHeaderContentStyle = (Style)Application.Current.Resources["HeaderStyle"],
    Binding = new Binding("ParamValue"),
    Width=50,
    Header="Value"
});

A bit of research pointed me to using Application.Current.Resources["HeaderStyle"] , however it returns null and so the style isn't applied to the header.

What am I doing wrong here?

My solution was to set the styles in code as well and assign them that way. Doesn't exactly answer the question as specified in the "Subject" of my post but it did the trick for me:

private Style _headerStyle;

// etc. etc. 
public SetColumns
{
    _headerStyle = new Style(typeof(HeaderContentControl));
    _headerStyle.Setters.Add(new Setter(Control.FontWeightProperty, FontWeights.Bold));

    ParamColumns.Add(new GridColumn
    {
        ColumnHeaderContentStyle = _headerStyle,
        FieldName = "ParamName",
        Width=80,
        Header="Parameter"
    });


    ParamColumns.Add(new GridColumn
    {
        ColumnHeaderContentStyle = _headerStyle,
        FieldName = "ParamValue",
        Width=50,
        Header="Value"
    });

Why don't you simply bind the 'FontWeight' Property to something in the Model or ViewModel..... that will maintain the MVVM pattern

<UserControl.Resources>
    <Style x:Key="HeaderStyle" TargetType="dxg:HeaderContentControl">
        <Setter Property="FontWeight" Value="{Binding FontWeightProp}" />
    </Style>
</UserControl.Resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM