简体   繁体   English

WPF 在 ViewModel 中设置 Style StaticResource

[英]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 .我在 WPF 中有一个 GridControl (它来自 DevExpress 但这不是重点),我正在尝试基于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.但是,我正在 ViewModel 中动态构建列,因此我需要能够在运行时以编程方式设置ColumnHeaderContentStyle

So the XAML has...所以 XAML 有...

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

... and in the C# Code... ...在 C# 代码中...

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.一些研究指出我使用Application.Current.Resources["HeaderStyle"] ,但它返回null ,因此样式不适用于标题。

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为什么不简单地将 'FontWeight' 属性绑定到 Model 或 ViewModel 中的某些东西..... 这将保持 MVVM 模式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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