Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用样式属性更改网格控件中复选标记的颜色_C#_Wpf_Xaml_Mvvm_Devexpress - Fatal编程技术网

C# 使用样式属性更改网格控件中复选标记的颜色

C# 使用样式属性更改网格控件中复选标记的颜色,c#,wpf,xaml,mvvm,devexpress,C#,Wpf,Xaml,Mvvm,Devexpress,我有一个使用MVVM设计模式的WPF应用程序。其中一个用户控件将网格控件绑定到视图模型中的数据表。网格控件的一列为bool类型。因此网格控件将它们显示为复选框列,使用户可以选中/取消选中复选框。现在我想要实现的是基于特定条件的网格控件的不同行的不同颜色。为此,我在我的UserControl的资源字典中定义了一个静态资源样式。代码如下: <UserControl.Resources> <ResourceDictionary> <Style x:Key

我有一个使用MVVM设计模式的WPF应用程序。其中一个用户控件将网格控件绑定到视图模型中的数据表。网格控件的一列为bool类型。因此网格控件将它们显示为复选框列,使用户可以选中/取消选中复选框。现在我想要实现的是基于特定条件的网格控件的不同行的不同颜色。为此,我在我的
UserControl
的资源字典中定义了一个静态资源样式。代码如下:

<UserControl.Resources>
   <ResourceDictionary>
      <Style x:Key="ForeroundStyle" TargetType="{x:Type dxg:GridRowContent}">
         <Setter Property="Foreground">
            <Setter.Value>
               <MultiBinding Converter ="{StaticResource MyConverter}" ConverterParameter="1">
                  <MultiBinding.Bindings>
                     <Binding Path="DataContext"/>
                     <Binding Path="Row"/>
                  </MultiBinding.Bindings>
               </MultiBinding>
            </Setter.Value>
         </Setter>
      </Style>
   </ResourceDictionary>
</UserControl.Resources>


我的问题是网格控件的所有其他列元素都通过转换器获得指定的颜色,但是复选框中的复选标记有一个默认颜色值,该值不会更改。如何实现复选框中复选标记的相同功能。任何建议都会有帮助

要更改复选框中复选标记的颜色,可以执行以下三个步骤

首先,通过以下方式添加新的dependecy属性来扩展CheckBox控件:

public class ExCheckBox : CheckBox
{
    public static readonly DependencyProperty CheckMarkColorProperty =
        DependencyProperty.Register("CheckMarkColor", typeof(Brush), typeof(ExCheckBox), new UIPropertyMetadata(Brushes.Black));

    public Brush CheckMarkColor
    {
        get
        {
            return (Brush)GetValue(CheckMarkColorProperty);
        }
        set
        {
            SetValue(CheckMarkColorProperty, value);
        }
    }
}
<Path Width="7" Height="7" 
    x:Name="CheckMark"
    SnapsToDevicePixels="False" 
    Stroke="{TemplateBinding CheckMarkColor}"
    StrokeThickness="2"
    Data="M 0 0 L 7 7 M 0 7 L 7 0" />
第二步:可以使用复选框样式。你可以找到一个样品。现在您只需将
CheckMarkColorProperty
绑定到“CheckMark”。在示例中,您需要以以下方式修改
路径
对象:

public class ExCheckBox : CheckBox
{
    public static readonly DependencyProperty CheckMarkColorProperty =
        DependencyProperty.Register("CheckMarkColor", typeof(Brush), typeof(ExCheckBox), new UIPropertyMetadata(Brushes.Black));

    public Brush CheckMarkColor
    {
        get
        {
            return (Brush)GetValue(CheckMarkColorProperty);
        }
        set
        {
            SetValue(CheckMarkColorProperty, value);
        }
    }
}
<Path Width="7" Height="7" 
    x:Name="CheckMark"
    SnapsToDevicePixels="False" 
    Stroke="{TemplateBinding CheckMarkColor}"
    StrokeThickness="2"
    Data="M 0 0 L 7 7 M 0 7 L 7 0" />

当然,样式必须“链接”到
ExCheckBox
控件

最后一步:可以在网格中绑定ExCheckBox,以便根据逻辑更改其复选标记颜色