【问题标题】:Base XAML Controls on a Resource Control基于资源控制的 XAML 控制
【发布时间】:2013-10-04 14:20:40
【问题描述】:

是否可以将一个或多个控件基于资源中定义的控件。以便控件从基本控件继承大多数属性。类似于 Style 方法,但由于某种原因我不能使用 Events/EventSetter(在这种情况下为AutoGeneratingColumns),我得到的错误是“xy-Event 不是路由事件”

这是我想要完成的示例。 我有大多数属性相同的数据网格

<DataGrid x:Name="gridEditSystem"
           AutoGeneratingColumn="onGridEditSystem_autoGeneratingColumn"
           SelectionUnit="Cell"
           SelectionMode="Single" CellStyle="{StaticResource GridEditStyle}">
</DataGrid>

<DataGrid x:Name="gridEditPlanets" SelectedCellsChanged="gridEditPlanets_SelectedCellsChanged"
           AutoGeneratingColumn="onGridEditSystem_autoGeneratingColumn"
           SelectionUnit="Cell"
           SelectionMode="Single" CellStyle="{StaticResource GridEditStyle}">
</DataGrid>

我现在想要的是一个“基本控制”

<Window.Resources>
    <DataGrid x:Key="BaseDataGrid" AutoGeneratingColumn="onGridEditSystem_autoGeneratingColumn"
              SelectionMode="Single" SelectionUnit="Cell"
              CellStyle="{StaticResource GridEditStyle}">
    </DataGrid>
</Window.Resources>

以及继承控件

 <DataGrid x:Name="gridEditSystem"
           BasedOn/Inherits/Templates={StaticResource BaseDataGrid}
 </DataGrid>

 <DataGrid x:Name="gridEditPlanets" 
           BasedOn/Inherits/Templates={StaticResource BaseDataGrid}
 </DataGrid>

我尝试了一些组合,但到目前为止都失败了,也没有在 Google 上找到任何东西。这在 XAML 中可行吗?

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    您不能这样做,但是在 WPF 中,您可以通过多种方式实现这一点。

    您可以使用所有常用属性制作自定义网格控件,并使用它来代替常规的DataGrid 控件。

        public class BaseDataGrid : DataGrid
        {
            protected override void OnInitialized(EventArgs e)
            { 
                 base.OnInitialized(e);
    
                 // Set all you common properties here
                 SelectionUnit = DataGridSelectionUnit.Cell;
                 SelectionMode = DataGridSelectionMode.Single;
                 CellStyle = FindResource("GridEditStyle") as Style;
             }
        }
    

    在您的 xaml 中

            <local:BaseDataGrid x:Name="gridEditSystem"/>
            <local:BaseDataGrid x:Name="gridEditPlanets"/>
    

    您还可以使用所有常见属性创建一个行为并将其附加到您想要的DataGrids。

         public class BaseGridBehavior : Behavior<DataGrid>
         {
            protected override void OnAttached()
            {
                AssociatedObject.Initialized += AssociatedObject_Initialized;
    
                 base.OnAttached();
            }
    
            void AssociatedObject_Initialized(object sender, EventArgs e)
            {
                // Set all you common properties here
                AssociatedObject.SelectionUnit = DataGridSelectionUnit.Cell;
                AssociatedObject.SelectionMode = DataGridSelectionMode.Single;
                AssociatedObject.CellStyle = AssociatedObject.FindResource("GridEditStyle") as Style;
            }
        }
    

    在 xaml 中:

            <DataGrid x:Name="gridEditSystem">
                <i:Interaction.Behaviors>
                    <local:BaseGridBehavior/>
                </i:Interaction.Behaviors>
            </DataGrid>
            <DataGrid x:Name="gridEditPlanets">
                <i:Interaction.Behaviors>
                    <local:BaseGridBehavior/>
                </i:Interaction.Behaviors>
            </DataGrid>
    

    这需要你包含和引用System.Windows.Interactivity dll

    希望对你有帮助

    【讨论】:

    • 谢谢。我已经想到了一个基类,但不太喜欢这个想法。用行为来做这件事对我来说是新的。看来我会满足于基类。
    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多