【问题标题】:Making the style's binding more flexible使样式的绑定更灵活
【发布时间】:2011-02-20 13:22:37
【问题描述】:

我正在尝试使现有样式更加灵活。 我正在附加一个部门。属性到数据网格行控件的双击事件。 在双击行时,我想要打开某个窗口。 为了实现这一点,我实现了一个 dep.属性并添加了样式定义。 以下是视图文件中的部分:

<Style x:Key="SomeKey" TargetType={x:Type DataGridRow} BasedOn= "SomeOtherKey">
    <Setter Property="vm:DataGridBehavior:OnDoubleClick" Value="{Binding Path=CommandName,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
    </Setter>
</Style>

附加的属性挂钩到数据行上的双击事件 并执行视图绑定的relay命令。 我想在风格上提供更多的灵活性 通过在视图本身中指定要调用的命令名称(因为它可能在不同视图之间有所不同)。 我怎样才能做到这一点? 有没有我遗漏的模板定义? 任何帮助将不胜感激:)

【问题讨论】:

    标签: wpf binding wpfdatagrid


    【解决方案1】:

    您可以继承 DataGrid 并为命令添加一个属性,例如 MyCommand。然后您可以在每个 DataGrid 中绑定该 ICommand 并使用 MyCommand 作为 RowStyle 绑定中的路径

    数据网格

    <local:CommandDataGrid RowStyle="{StaticResource SomeKey}"
                           MyCommand="{Binding CommandName1}">
    <!-- ... -->
    <local:CommandDataGrid RowStyle="{StaticResource SomeKey}"
                           MyCommand="{Binding CommandName2}">
    

    行样式

    <Style x:Key="SomeKey" TargetType="{x:Type DataGridRow}">
        <Setter Property="vm:DataGridBehavior.OnDoubleClick"
                Value="{Binding Path=MyCommand,
                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
    </Style>
    

    CommandDataGrid

    public class CommandDataGrid : DataGrid
    {
        public static readonly DependencyProperty MyCommandProperty =
            DependencyProperty.Register("MyCommand",
                                        typeof(ICommand),
                                        typeof(CommandDataGrid),
                                        new UIPropertyMetadata(null));
    
        public ICommand MyCommand
        {
            get { return (ICommand)GetValue(MyCommandProperty); }
            set { SetValue(MyCommandProperty, value); }
        }
    }
    

    或者,您可以创建一个附加属性,例如您使用的 MyCommand

    数据网格

    <DataGrid vm:DataGridBehavior.MyCommand="{Binding CommandName}"
    

    行样式

    <Style x:Key="SomeKey" TargetType="{x:Type DataGridRow}">
        <Setter Property="vm:DataGridBehavior.OnDoubleClick"
                Value="{Binding Path=(vm:DataGridBehavior.MyCommand),
                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
    </Style>
    

    我的命令属性

    public static readonly DependencyProperty MyCommandProperty =
        DependencyProperty.RegisterAttached("MyCommand",
                                            typeof(ICommand),
                                            typeof(DataGridBehavior),
                                            new UIPropertyMetadata(null));
    public static void SetMyCommand(DependencyObject element, ICommand value)
    {
        element.SetValue(MyCommandProperty, value);
    }
    public static ICommand GetMyCommand(DependencyObject element)
    {
        return (ICommand)element.GetValue(MyCommandProperty);
    }
    

    【讨论】:

    • 首先,非常感谢 :) 这行得通,太棒了。我想知道您是否能想到任何非子类解决方案,使我能够保持视图的 xaml 完整(仍然使用 DataGrid 而不是另一个子类)?
    • @Gena Verdel:查看我的更新答案。您可以使用附加属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多