【问题标题】:WPF use Datagrid shared resourcesWPF 使用 Datagrid 共享资源
【发布时间】:2013-08-20 18:06:09
【问题描述】:

我在所有数据网格中都有一些样式可应用于某些类型,但特别是在数据网格内部,所以它看起来像这样:

                        <DataGrid.Resources>
                            <Style TargetType="{x:Type DataGridCell}">
                                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
                                <Style.Triggers>
                                    <Trigger Property="IsFocused" Value="True">
                                        <Setter Property="BorderBrush" Value="White"/>
                                        <Setter Property="BorderThickness" Value="2"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                            <Style TargetType="{x:Type vmcc:LOVComboBox}">
                                <EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
                            </Style>
                            <Style TargetType="{x:Type TextBox}">
                                <EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
                            </Style>
                        </DataGrid.Resources>

如何在我的应用程序(例如在样式文件中)的一个点中创建 xaml 代码以执行相同的操作,将其应用于所有数据网格?例如,我已经为文本框完成了样式,但这适用于数据网格内的元素......

【问题讨论】:

    标签: wpf datagrid resources styles code-reuse


    【解决方案1】:

    因为您可能希望在样式资源中包含事件处理程序,所以我建议使用后面的代码创建 ResourceDictionary。只需在 Visual Studio 中创建 UserControl 并替换其中的代码 - 您将拥有完美嵌套的 *.xaml 和 *.xaml.cs 文件。为了在整个应用程序中应用 DataGrid 的样式,请将它们包含在 App.xaml 中的应用程序资源中:

    <Application.Resources>
        <ResourceDictionary Source="MyStyles.xaml"/>
    </Application.Resources>
    

    MyStyles.xaml 文件中的代码:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        x:Class="Example.MyStyles"
                        xmlns:vmcc="clr-namespace:Example">
    
        <Style TargetType="{x:Type DataGridCell}" x:Key="DataGrid.DataGridCellStyle">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="BorderBrush" Value="White" />
                    <Setter Property="BorderThickness" Value="2" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type vmcc:LOVComboBox}" x:Key="DataGrid.LOVComboBoxStyle">
            <EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
        </Style>
        <Style TargetType="{x:Type TextBox}" x:Key="DataGrid.TextBoxStyle">
            <EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
        </Style>
    
        <Style TargetType="{x:Type DataGrid}">
            <Style.Resources>
                <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGrid.DataGridCellStyle}" />
                <Style TargetType="vmcc:LOVComboBox" BasedOn="{StaticResource DataGrid.LOVComboBoxStyle}" />
                <Style TargetType="TextBox" BasedOn="{StaticResource DataGrid.TextBoxStyle}" />
            </Style.Resources>
        </Style>
    </ResourceDictionary>
    

    我们希望 TextBox、LOVComboBox 样式仅应用于 DataGrid 内部 - 这就是为什么它们的样式包含在 DataGrid 样式的 Style.Resources 中的原因。我试图把从你的代码中复制的样式(没有 x:Key 属性)放在那里,但我得到了一个错误: 无法在样式中的目标标记上指定事件“PreviewMouseLeftButtonDown”。请改用 EventSetter。。这就是为什么我将它们提取为带有键的资源,并在 DataGrid 样式的 Style.Resources 中使用派生样式(建议 here)。

    MyStyles.xaml.cs 文件中的代码:

    public partial class MyStyles
    {
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //your code here
        }
    
        private void UIElement_GotFocus(object sender, RoutedEventArgs e)
        {
            //your code here
        }
    }
    

    【讨论】:

    • 完美运行,感谢代码的完整解释!
    【解决方案2】:

    您可以创建一个资源字典,在一个集中位置包含这些样式,然后在每次创建网格时引用此文件。有一篇很好的文章概述了资源字典here

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2011-02-10
      • 1970-01-01
      • 2021-10-10
      • 2015-10-27
      • 1970-01-01
      • 2022-12-12
      • 2023-02-07
      相关资源
      最近更新 更多