2016-02-28 86 views
0

我知道,因爲DataGrid列不是Visual Tree的一部分,所以不能將列的可見性屬性直接綁定到虛擬機中的布爾屬性。你必須以另一種方式來做。下面是我做的方式:將可見性綁定到WPF dataGrid中的bool值

public class LocalVm 
{ 
    public static ObservableCollection<Item> Items 
    { 
     get 
     { 
      return new ObservableCollection<Item> 
      { 
       new Item{Name="Test1", ShortDescription = "Short1"} 
      }; 
     } 
    } 

    public static bool IsDetailsModeEnabled 
    { 
     get { return true; } 
    } 
} 

public class Item : INotifyPropertyChanged 
{ 
    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      OnPropertyChanged(); 
     } 
    } 


    private string _shortDescription; 

    public string ShortDescription 
    { 
     get 
     { 
      return _shortDescription; 
     } 
     set 
     { 
      _shortDescription = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

XAML:

<Window x:Class="Wpf.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:Wpf" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     d:DataContext="{d:DesignInstance Type=local:LocalVm}" 
     Title="MainWindow" Height="350" Width="525" 
     Name="MyWindow"> 

    <Window.Resources> 
     <BooleanToVisibilityConverter x:Key="BoolToVis" /> 

     <DataGridTextColumn x:Key="ThatPeskyColumn" 
          Binding="{Binding Size}" 
          Visibility="{Binding DataContext.IsDetailsModeEnabled, 
          Source={x:Reference MyWindow}, 
          Converter={StaticResource BoolToVis}}"/> 

    </Window.Resources> 

    <Grid> 
     <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Name}" /> 
       <StaticResource ResourceKey="ThatPeskyColumn"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

然而,在我的XAML窗口,有對「可見性」屬性的錯誤:「對象未設置到一個對象的實例「。如果我刪除了源代碼和轉換器部分,錯誤會消失,但它不能正確綁定。我做錯了什麼?

在此先感謝

回答

2

,我可以從你所提供的代碼中看到,IsDetailsModeEnabled屬性是靜態的。綁定到靜態屬性在這種情況下,你可能只是創建虛擬機的靜態資源,結合其靜態屬性,並將其設置爲窗口的DataContext後:

<Window.Resources> 
    <local:LocalVm x:Key="vm" /> 
    <BooleanToVisibilityConverter x:Key="BoolToVis" /> 

    <DataGridTextColumn x:Key="ThatPeskyColumn" 
         Binding="{Binding ShortDescription}" 
         Visibility="{Binding Path = IsDetailsModeEnabled, 
         Source={StaticResource vm}, 
         Converter={StaticResource BoolToVis}}"/> 
</Window.Resources> 
<Window.DataContext> 
    <StaticResource ResourceKey="vm"/> 
</Window.DataContext> 

從另一方面,在更多的經典方法情況下將是與代理可凍結對象,描述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)); 
} 

XAML

<Window.Resources> 
    <local:BindingProxy x:Key="proxy" Data="{Binding}" /> 
    <BooleanToVisibilityConverter x:Key="BoolToVis" /> 

    <DataGridTextColumn x:Key="ThatPeskyColumn" 
         Binding="{Binding ShortDescription}" 
         Visibility="{Binding Path=Data.IsDetailsModeEnabled, 
         Source={StaticResource proxy}, 
         Converter={StaticResource BoolToVis}}"/> 
</Window.Resources> 
相關問題