2011-01-25 53 views
1

我想用setter設置WPF數據網格中按鈕的命令。但似乎在我返回 CreateInstanceCore()中的一個副本後,DP屬性CommandProperty獲得了其默認值null的過度寫入,因此原始命令會丟失。DataGrid CellStyle Setter with Freezable StaticRecource

如果我直接綁定StaticResource它沒有問題。 有沒有辦法阻止該行爲或其他解決方案?

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:

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

回答

0

如果你定義你的資源命令,這樣它的工作原理:

<local:ResourceCommand x:Key="FirstCommand" Command="{Binding FirstCommand}" x:Shared="False"/> 

使用這種技術,你甚至可以扔在CreateInstanceCore沒有實現,所以你」我只是使用Freezable來啓用數據綁定。

+0

1.在DataGrid和Command之外的按鈕綁定爲無樣式: Works。但之前也工作過。 2.使用樣式設置器的DataGrid和命令設置之外的按鈕: 不起作用。調用CreateInstanceCore()。 2. DataGrid和Command中的按鈕綁定時沒有樣式或設置Command over style: 不起作用。調用CreateInstanceCore()。 – 2011-01-27 07:46:22