以下方法只能实现一次绑定,如果MyVisible属性在ViewModel中被修改的话,不会影响DataGrid的Column的显示或者隐藏
<Window x:Class="DataGridColumnVisibilityBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridColumnVisibilityBinding"
Title="MainWindow" Height="350" Width="525" x:Name="window">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.Resources>
<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
</Grid.Resources>
<ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"/>
<ContentControl Visibility="Collapsed" x:Name="TestProxy" DataContext="{Binding}"/>
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Name="dataGrid1" ItemsSource="{Binding MockData}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" ></DataGridTextColumn>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" ></DataGridTextColumn>
<DataGridTextColumn Header="Message" Binding="{Binding Message}"
Visibility="{Binding DataContext.MyVisible, Source={StaticResource ProxyElement}}">
</DataGridTextColumn>
<DataGridTextColumn Header="Message2" Binding="{Binding Message}"
Visibility="{Binding Source={x:Reference TestProxy}, Path=DataContext.MyVisible}">
</DataGridTextColumn>
<DataGridTextColumn Header="Message3" Binding="{Binding Message}"
Visibility="{Binding Source={x:Reference testButton}, Path=Visibility}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Button Content="testButton" Height="23" Name="testButton" Width="99" Visibility="Collapsed"/>
</StackPanel>
</Grid>
</Window>
后台代码
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private IList<DataGridItem> mockData;
public IList<DataGridItem> MockData
{
get
{
if (mockData == null)
{
mockData = new List<DataGridItem>();
mockData.Add(new DataGridItem("name1", "description_001", "message_001"));
mockData.Add(new DataGridItem("name_002", "description_002", "message_002"));
mockData.Add(new DataGridItem("name3", "description_003", "message_003"));
mockData.Add(new DataGridItem("name_004", "description_004", "message_004"));
mockData.Add(new DataGridItem("name5", "description_005", "message_005"));
mockData.Add(new DataGridItem("name_006", "description_006", "message_006"));
mockData.Add(new DataGridItem("name7", "description_007", "message_007"));
mockData.Add(new DataGridItem("name_008", "description_008", "message_008"));
mockData.Add(new DataGridItem("name9", "description_009", "message_009"));
}
return mockData;
}
}
private Visibility myVisible;
public Visibility MyVisible
{
get
{
return Visibility.Visible;
}
set
{
myVisible = value;
}
}
}
public class DataGridItem
{
public DataGridItem(string name, string description, string message)
{
this.Name = name;
this.Description = description;
this.Message = message;
}
public string Name { get; set; }
public string Description { get; set; }
public string Message { get; set; }
}