0

是否可以将一个或多个控件基于资源中定义的控件。以便控件从基本控件继承大多数属性。类似于 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>

并继承 Controls

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

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

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

4

1 回答 1

0

您不能这样做,但是在 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;
         }
    }

在你的xml中

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

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

     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.Interactivitydll

希望这可以帮助

于 2013-10-04T15:38:43.560 回答