如何通过 materialdesign:MaterialDataGridTextColumn 上的绑定设置前景
How set Foreground with binding on materialdesign:MaterialDataGridTextColumn
如果我在 xaml:
中使用该语法,则无法使用绑定设置列单元格的前景
<materialDesign:MaterialDataGridTextColumn Foreground="{Binding Foreground}"/>
在输出终端加载 windows 我看到:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Foreground; DataItem=null; target element is 'MaterialDataGridTextColumn' (HashCode=29149718); target property is 'Foreground' (type 'Brush')
如果我把直接颜色举例:
<materialDesign:MaterialDataGridTextColumn Foreground="Red"/>
Datagrid Cell 正确渲染前景,有人有什么建议吗?
此致
佛朗哥
我找到了解决方案!
我必须使用 ElementStyle 属性:
<DataGridTextColumn Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding Foreground}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
我希望这对其他人有用。
此致
佛朗哥
如果我在 xaml:
中使用该语法,则无法使用绑定设置列单元格的前景<materialDesign:MaterialDataGridTextColumn Foreground="{Binding Foreground}"/>
在输出终端加载 windows 我看到:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Foreground; DataItem=null; target element is 'MaterialDataGridTextColumn' (HashCode=29149718); target property is 'Foreground' (type 'Brush')
如果我把直接颜色举例:
<materialDesign:MaterialDataGridTextColumn Foreground="Red"/>
Datagrid Cell 正确渲染前景,有人有什么建议吗?
此致
佛朗哥
我找到了解决方案!
我必须使用 ElementStyle 属性:
<DataGridTextColumn Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding Foreground}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
我希望这对其他人有用。
此致
佛朗哥