2016-02-29 67 views
0

我創建ControlTemplates DataGrid的按鈕觸發:在DataGridDataTrigger綁定到財產的視圖模型不

<Window.Resources> 
    <ControlTemplate x:Key="imgNo" TargetType="{x:Type Control}"> 
     <Image Source="pack://application:,,,/Images/up.png"/> 
    </ControlTemplate> 

    <ControlTemplate x:Key="imgUp" TargetType="{x:Type Control}"> 
     <!--<TextBlock Text="Up"/>--> 
     <Image Source="pack://application:,,,/Images/up.png"/> 
    </ControlTemplate> 

    <ControlTemplate x:Key="imgDown" TargetType="{x:Type Control}"> 
     <Image Source="pack://application:,,,/Images/downArrow.png"/> 
    </ControlTemplate> 

    <DataTemplate x:Key="ButtonOneDataTemplate"> 
     <Control x:Name="theControl" Template="{DynamicResource imgNo}" /> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding IsImageChanged}" Value="true"> 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource imgUp}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding IsImageChanged}" Value="false"> 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource imgDown}" /> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</WindowResources> 

Button它使用上述ControlTemplates

<DataGrid ItemsSource="{Binding Persons}" Grid.Row="1" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Binding="{Binding IdPerson}"> 
     <DataGridTextColumn.HeaderTemplate>       
      <DataTemplate> 
      <Border Background="Violet"> 
      <StackPanel> 
       <Button ContentTemplate="{StaticResource ButtonOneDataTemplate}" 
       Command="{Binding DataContext.HelloCommand, RelativeSource= 
        {RelativeSource AncestorType=Window}}" 
        CommandParameter="{Binding DataContext.Hello, 
       RelativeSource={RelativeSource AncestorType=DataGrid}}"/> 
      </StackPanel> 
      </Border> 
     </DataTemplate> 
     </DataGridTextColumn.HeaderTemplate>     
    </DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid>      

ViewModel

public class MainWindowViewModel:ViewModelBase 
{ 
    public RelayCommand HelloCommand { get; set; } 
    public MainWindowViewModel() 
    { 
     LoadPersons(); 
     HelloCommand = new RelayCommand(SayHello); 
    } 

    int helloCounter = 0; 
    private void SayHello(object obj) 
    { 
     if (helloCounter % 2 == 0) 
      IsImageChanged = true; 
     else 
      IsImageChanged = false;   
     helloCounter++; 
    } 


    private bool isImageChanged=true; 
    public bool IsImageChanged 
    { 
     get { return isImageChanged; } 
     set { isImageChanged = value; 
      OnPropertyChanged("IsImageChanged"); 
      } 
    } 
} 

我想要的是當我點擊按鈕<Button ContentTemplate="{StaticResource ButtonOneDataTemplate}"/>,然後Template應該被替換爲{DynamicResource imgDown}{DynamicResource imgUp}DataTrigger取決於IsImageChanged的值。

但是,如果我點擊Button,然後DataTrigger不解僱(ControltemplatesimgUpimgDown沒有變化)。我怎樣才能從我的ViewModel實現這一點?

+0

請提供完整的viewmodel代碼。 。 – Gopichandar

+0

你仍然不清楚目標。你試圖用這個來實現什麼? – Gopichandar

+0

@Gopichandar請參閱我更新的問題 – StepUp

回答

1

問題是DataGrid列不是可視化樹的一部分,因此它不會繼承DataContext。爲了能夠在你的ButtonOneDataTemplate中使用DataTrigger,你需要這個按鈕,你應用這個模板,並且有正確的DataContext。還有一招,如何提供給的DataContext是不是的VisualTree元素,描述here

應用該解決方案對你的代碼,我們將有以下幾點:

代理

public class BindingProxy : Freezable 
{ 
    #region Overrides of Freezable 

    protected override Freezable CreateInstanceCore() 
    { 
     return new BindingProxy(); 
    } 

    #endregion 

    public object Data 
    { 
     get { return (object)GetValue(DataProperty); } 
     set { SetValue(DataProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty DataProperty = 
     DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); 
} 

窗口。資源

<local:BindingProxy x:Key="proxy" Data="{Binding}" /> 

    <DataTemplate x:Key="ButtonOneDataTemplate"> 
     <Control x:Name="theControl" Template="{DynamicResource imgNo}" Foreground="Orange"/> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext.IsImageChanged}" Value="True"> 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource imgUp}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext.IsImageChanged}" Value="False"> 
       <Setter TargetName="theControl" Property="Template" Value="{DynamicResource imgDown}" /> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

HeaderTemplate中

<DataGridTextColumn.HeaderTemplate> 
    <DataTemplate> 
     <Border Background="Violet"> 
      <StackPanel> 
       <Button ContentTemplate="{StaticResource ButtonOneDataTemplate}" 
         DataContext="{Binding Path=Data, Source={StaticResource proxy}}" 
         Command="{Binding DataContext.ButtonClick, RelativeSource={RelativeSource AncestorType=Window}}" 
         CommandParameter="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}"/> 
      </StackPanel> 
     </Border> 
    </DataTemplate> 
</DataGridTextColumn.HeaderTemplate> 
+0

萬謝謝,Man!這真的很有用!:)非常感謝你! – StepUp

+1

歡迎您:) –