首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Freezable StaticRecource的DataGrid CellStyle设定器

使用Freezable StaticRecource的DataGrid CellStyle设定器
EN

Stack Overflow用户
提问于 2011-01-25 21:49:46
回答 1查看 524关注 0票数 1

我想用setter在WPF数据网格中设置按钮的命令。但是,在我在CreateInstanceCore()中返回一个副本后,DP属性CommandProperty似乎用它的默认值null进行了改写,因此原始命令丢失了。

如果我直接绑定StaticResource,它可以正常工作。有没有办法阻止这种行为或另一种解决方案?

代码语言:javascript
运行
复制
public class ResourceCommand : Freezable, ICommand {

    public ICommand Command {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(ResourceCommand), new UIPropertyMetadata(null, CommandPropertyChangedCallback));


    static void CommandPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ResourceCommand resourceCommand = (ResourceCommand)d;
        int h = resourceCommand.GetHashCode();
        if (e.OldValue != null)
            ((ICommand)e.OldValue).CanExecuteChanged -= resourceCommand.OnCanExecuteChanged;
        if (e.NewValue != null)
            ((ICommand)e.NewValue).CanExecuteChanged += resourceCommand.OnCanExecuteChanged;
    }

    #region ICommand Member

    public bool CanExecute(object parameter) {
        if (Command == null)
            return false;
        return Command.CanExecute(parameter);
    }

    public event EventHandler CanExecuteChanged;

    void OnCanExecuteChanged(object sender, EventArgs e) {
        if (CanExecuteChanged != null)
            CanExecuteChanged(sender, e);
    }

    public void Execute(object parameter) {
        Command.Execute(parameter);
    }

    #endregion

    protected override Freezable CreateInstanceCore() {
        ResourceCommand ResourceCommand = new ResourceCommand();
        ResourceCommand.Command = Command;
        return ResourceCommand;
    }
}

xaml:

代码语言:javascript
运行
复制
<Window.Resources>
    <local:ResourceCommand x:Key="FirstCommand"  Command="{Binding FirstCommand}" />
    <local:ResourceCommand x:Key="SecondCommand"  Command="{Binding SecondCommand}" />
</Window.Resources>
<Grid>
    <DataGrid ItemsSource="{Binding Collection}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Click me">
                            <Button.Style>
                                <Style TargetType="Button">
                                    <Setter Property="Command" Value="{StaticResource FirstCommand}" />
                                </Style>
                            </Button.Style></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-27 09:17:06

如果你像这样定义你的资源命令,它就会起作用:

代码语言:javascript
运行
复制
<local:ResourceCommand x:Key="FirstCommand" Command="{Binding FirstCommand}" x:Shared="False"/>

使用这种技术,您甚至可以抛出未在CreateInstanceCore中实现的异常,因此您只需使用Freezable来启用数据绑定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4794259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档