Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# Datagrid行样式绑定到动态更改的属性_C#_Wpf_Datagrid - Fatal编程技术网

C# Datagrid行样式绑定到动态更改的属性

C# Datagrid行样式绑定到动态更改的属性,c#,wpf,datagrid,C#,Wpf,Datagrid,大家好:我正在尝试设计datagrid行的样式。流程是基于网格中的字段(MsgType),我需要为行着色。颜色(前后)由用户在preferences屏幕中进行配置,我将此配置保存在每个MsgType的可观察集合中。用户可以通过首选项屏幕更改此配置。我需要将颜色(可以更改)绑定到该MsgType的行。我尝试了以下方法,绑定只在第一次起作用……因为MsgType没有改变。颜色由转换器根据msgtype获取 <DataGrid.RowStyle> <Style TargetT

大家好:我正在尝试设计datagrid行的样式。流程是基于网格中的字段(MsgType),我需要为行着色。颜色(前后)由用户在preferences屏幕中进行配置,我将此配置保存在每个MsgType的可观察集合中。用户可以通过首选项屏幕更改此配置。我需要将颜色(可以更改)绑定到该MsgType的行。我尝试了以下方法,绑定只在第一次起作用……因为MsgType没有改变。颜色由转换器根据msgtype获取

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
      <Setter Property="Background" Value="{Binding MsgType, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource MessageTypeToBackConverter}}"/>
      <Setter Property="Foreground" Value="{Binding MsgType, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource MessageTypeToForeConverter}}"/>
    </Style>
</DataGrid.RowStyle>

任何帮助都将不胜感激。我觉得我可能采取了错误的方法


谢谢,

根据您的出价,这个行为非常好

你有两个选择

保持代码原样,每当首选项更改时,调用
DataGrid.Items.Refresh()
或重新绑定
DataGrid.ItemsSource
属性以重新生成
DataGridRows
。这样,它们将受更改的首选项的影响(即bg和fg颜色将刷新)

更好的方法是修改逻辑,使用基于
DynamicResources
画笔
通过
触发器
DataGridRows
着色,假设
MsgType
值在编译时是有限且众所周知的

例如

假设您的
MsgType
是某个枚举,因此它根据它所代表的值的数量得到了很好的定义

public enum MsgType
{
  None = 0,
  Read = 1,
  Edit = 2,
  Delete = 3
}
XAML

 <DataGrid.Resources>
       <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="MyDefaultPreferences.xaml"/>
          </ResourceDictionary.MergedDictionaries>              
       </ResourceDictionary>
 </DataGrid.Resources>
 ...
 <DataGrid.RowStyle>
<Style TargetType="DataGridRow">
     <Setter property="BackgroundColor" Value="{DynamicResource NoneMsgTypeBrush}" />
     <Style.Triggers>
         <DataTrigger Binding="{Binding MsgType}" Value="1">
             <Setter Property="BackgroundColor" Value="{DynamicResource ReadMsgTypeBrush}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding MsgType}" Value="2">
             <Setter Property="BackgroundColor" Value="{DynamicResource EditMsgTypeBrush}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding MsgType}" Value="3">
             <Setter Property="BackgroundColor" Value="{DynamicResource DeleteMsgTypeBrush}" />
         </DataTrigger>
     </Style.Triggers>
</Style>
因此,在运行时,当用户更改首选项时,只需在
MyDefaultPreferences.xaml
(它是一个
字典
)中删除并添加具有相同键的
笔刷

例如

若用户将Read Msg Type更改为黄色,则您只需执行以下操作

 Uri uri = new Uri("MyDefaultPreferences.xaml", UriKind.RelativeOrAbsolute);
 var _myDefaultResourceDictionary = Application.LoadComponent(uri) as ResourceDictionary;
 _myDefaultResourceDictionary.Remove("ReadMsgTypeBrush");
 _myDefaultResourceDictionary.Add("ReadMsgTypeBrush", new SolidColorBrush(Colors.Yellow));
删除和添加画笔资源会更改通过
DynamicResource
引用的任何地方的颜色(即在相应的
SolidColorBrush
中)


这是在运行时更改WPF应用程序的皮肤\主题的一种非常常见的方法。

根据您的出价,该行为工作得非常好

你有两个选择

保持代码原样,每当首选项更改时,调用
DataGrid.Items.Refresh()
或重新绑定
DataGrid.ItemsSource
属性以重新生成
DataGridRows
。这样,它们将受更改的首选项的影响(即bg和fg颜色将刷新)

更好的方法是修改逻辑,使用基于
DynamicResources
画笔
通过
触发器
DataGridRows
着色,假设
MsgType
值在编译时是有限且众所周知的

例如

假设您的
MsgType
是某个枚举,因此它根据它所代表的值的数量得到了很好的定义

public enum MsgType
{
  None = 0,
  Read = 1,
  Edit = 2,
  Delete = 3
}
XAML

 <DataGrid.Resources>
       <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="MyDefaultPreferences.xaml"/>
          </ResourceDictionary.MergedDictionaries>              
       </ResourceDictionary>
 </DataGrid.Resources>
 ...
 <DataGrid.RowStyle>
<Style TargetType="DataGridRow">
     <Setter property="BackgroundColor" Value="{DynamicResource NoneMsgTypeBrush}" />
     <Style.Triggers>
         <DataTrigger Binding="{Binding MsgType}" Value="1">
             <Setter Property="BackgroundColor" Value="{DynamicResource ReadMsgTypeBrush}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding MsgType}" Value="2">
             <Setter Property="BackgroundColor" Value="{DynamicResource EditMsgTypeBrush}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding MsgType}" Value="3">
             <Setter Property="BackgroundColor" Value="{DynamicResource DeleteMsgTypeBrush}" />
         </DataTrigger>
     </Style.Triggers>
</Style>
因此,在运行时,当用户更改首选项时,只需在
MyDefaultPreferences.xaml
(它是一个
字典
)中删除并添加具有相同键的
笔刷

例如

若用户将Read Msg Type更改为黄色,则您只需执行以下操作

 Uri uri = new Uri("MyDefaultPreferences.xaml", UriKind.RelativeOrAbsolute);
 var _myDefaultResourceDictionary = Application.LoadComponent(uri) as ResourceDictionary;
 _myDefaultResourceDictionary.Remove("ReadMsgTypeBrush");
 _myDefaultResourceDictionary.Add("ReadMsgTypeBrush", new SolidColorBrush(Colors.Yellow));
删除和添加画笔资源会更改通过
DynamicResource
引用的任何地方的颜色(即在相应的
SolidColorBrush
中)


这是在运行时更改WPF应用程序的皮肤\主题的一种非常常见的方法。

感谢WPF it,作为一种快速解决方案,我使用了Items.Refresh()解决方案,效果很好。我将稍微更改代码以使用DynamicResources,以便正确完成。非常感谢您的快速回复。感谢WPF it,作为一个快速解决方案,我使用了Items.Refresh()解决方案,效果很好。我将稍微更改代码以使用DynamicResources,以便正确完成。非常感谢您的快速回复。