如何在 DevExpress GridControl CellTemplate 中绑定实际的主题名称
How to bind the actual theme name in DevExpress GridControl CellTemplate
我有一个 GridControl,我在单元格内使用一些带有几个按钮的单元格编辑。
这是一个包含 3 个按钮的示例列:
xmlns:dxet="http://schemas.devexpress.com/winfx/2008/xaml/editors/themekeys"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:my="clr-namespace:MyProject.DevExpress.View"
<dxg:GridColumn Width="Auto"
AllowEditing="False"
AutoFilterCondition="Default"
FieldName="Information"
Header="Info"
ShowInColumnChooser="False"
VisibleIndex="1">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ButtonEdit Name="PART_Editor" AllowDefaultButton="False">
<dxe:ButtonEdit.Buttons>
<!-- Button Action 1 -->
<my:ButtonInfoCustom Tag="Info1">
<my:ButtonInfoCustom.Template>
<DataTemplate>
<Button x:Name="PART_Item"
Height="Auto"
Content="{Binding DataContext.RowData.Row.InfoName}"
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle,
ThemeName=DeepBlue}}"
ToolTip="{Binding DataContext.RowData.Row.CustomTooltip}" />
</DataTemplate>
</my:ButtonInfoCustom.Template>
</my:ButtonInfoCustom>
<!-- Button Action 2 -->
<my:ButtonInfoCustom Tag="Info2">
...
问题是主题。
如果我明确设置主题名称,它可以正常工作:
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle, ThemeName=DeepBlue}}"
如果我尝试绑定:
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle, ThemeName={Binding ThemeToUse}}}"
我明白了:
XamlParseException: A 'Binding' cannot be set on the 'ThemeName'
property of type 'ButtonsThemeKeyExtension'. A 'Binding' can only be
set on a DependencyProperty of a DependencyObject.
我该怎么办?
好吧,你 不应该 将任何主题名称放入你的 XAML 代码中。
而是使用 DevExpress 内置控件,因为它们会考虑实际主题并相应地更改外观。
查看此示例(我完全删除了 Style
标记并将 Button
替换为 SimpleButton
):
<dxg:GridColumn Width="Auto"
AllowEditing="False"
AutoFilterCondition="Default"
FieldName="Information"
Header="Info"
ShowInColumnChooser="False"
VisibleIndex="1">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ButtonEdit Name="PART_Editor" AllowDefaultButton="False">
<dxe:ButtonEdit.Buttons>
<!-- Button Action 1 -->
<my:ButtonInfoCustom Tag="Info1">
<my:ButtonInfoCustom.Template>
<DataTemplate>
<dx:SimpleButton x:Name="PART_Item"
Height="Auto"
Content="{Binding DataContext.RowData.Row.InfoName}" ToolTip="{Binding DataContext.RowData.Row.CustomTooltip}" />
</DataTemplate>
</my:ButtonInfoCustom.Template>
</my:ButtonInfoCustom>
<!-- Button Action 2 -->
<my:ButtonInfoCustom Tag="Info2">
...
我有一个 GridControl,我在单元格内使用一些带有几个按钮的单元格编辑。
这是一个包含 3 个按钮的示例列:
xmlns:dxet="http://schemas.devexpress.com/winfx/2008/xaml/editors/themekeys"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:my="clr-namespace:MyProject.DevExpress.View"
<dxg:GridColumn Width="Auto"
AllowEditing="False"
AutoFilterCondition="Default"
FieldName="Information"
Header="Info"
ShowInColumnChooser="False"
VisibleIndex="1">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ButtonEdit Name="PART_Editor" AllowDefaultButton="False">
<dxe:ButtonEdit.Buttons>
<!-- Button Action 1 -->
<my:ButtonInfoCustom Tag="Info1">
<my:ButtonInfoCustom.Template>
<DataTemplate>
<Button x:Name="PART_Item"
Height="Auto"
Content="{Binding DataContext.RowData.Row.InfoName}"
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle,
ThemeName=DeepBlue}}"
ToolTip="{Binding DataContext.RowData.Row.CustomTooltip}" />
</DataTemplate>
</my:ButtonInfoCustom.Template>
</my:ButtonInfoCustom>
<!-- Button Action 2 -->
<my:ButtonInfoCustom Tag="Info2">
...
问题是主题。
如果我明确设置主题名称,它可以正常工作:
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle, ThemeName=DeepBlue}}"
如果我尝试绑定:
Style="{DynamicResource {dxet:ButtonsThemeKey ResourceKey=ButtonStyle, ThemeName={Binding ThemeToUse}}}"
我明白了:
XamlParseException: A 'Binding' cannot be set on the 'ThemeName' property of type 'ButtonsThemeKeyExtension'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
我该怎么办?
好吧,你 不应该 将任何主题名称放入你的 XAML 代码中。
而是使用 DevExpress 内置控件,因为它们会考虑实际主题并相应地更改外观。
查看此示例(我完全删除了 Style
标记并将 Button
替换为 SimpleButton
):
<dxg:GridColumn Width="Auto"
AllowEditing="False"
AutoFilterCondition="Default"
FieldName="Information"
Header="Info"
ShowInColumnChooser="False"
VisibleIndex="1">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ButtonEdit Name="PART_Editor" AllowDefaultButton="False">
<dxe:ButtonEdit.Buttons>
<!-- Button Action 1 -->
<my:ButtonInfoCustom Tag="Info1">
<my:ButtonInfoCustom.Template>
<DataTemplate>
<dx:SimpleButton x:Name="PART_Item"
Height="Auto"
Content="{Binding DataContext.RowData.Row.InfoName}" ToolTip="{Binding DataContext.RowData.Row.CustomTooltip}" />
</DataTemplate>
</my:ButtonInfoCustom.Template>
</my:ButtonInfoCustom>
<!-- Button Action 2 -->
<my:ButtonInfoCustom Tag="Info2">
...