1
A
回答
4
這一個是有點棘手。問題來自DataGrid.Columns只是一個屬性而不是可視化樹的一部分。這意味着像ElementName或RelativeSource這樣的常規綁定工具將不起作用。但是,如果您覆蓋DataGrid.DataContext屬性的元數據,則可以使其正常工作。下面的示例代碼:
<Window x:Class="DataGridColumnVisibilitySample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:l="clr-namespace:DataGridColumnVisibilitySample"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<l:VisibilityConverter x:Key="VisibilityC" />
</Window.Resources>
<DockPanel LastChildFill="True">
<CheckBox DockPanel.Dock="Top" Margin="8" Content="Show Column B" IsChecked="{Binding ShowColumnB}" />
<tk:DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" CanUserAddRows="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Column A" Binding="{Binding ColumnA}" />
<tk:DataGridTextColumn Header="Column B" Binding="{Binding ColumnB}"
Visibility="{Binding (FrameworkElement.DataContext).ShowColumnB,
RelativeSource={x:Static RelativeSource.Self},
Converter={StaticResource VisibilityC}}" />
<tk:DataGridTextColumn Header="Column C" Binding="{Binding ColumnC}" />
</tk:DataGrid.Columns>
</tk:DataGrid>
</DockPanel>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using Microsoft.Windows.Controls;
namespace DataGridColumnVisibilitySample
{
public partial class Window1 : INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
new DataGridContextHelper(); // Initialize Helper
Items = Enumerable.Range(1, 3).Select(i => new MyItem {ColumnA = "A" + i, ColumnB = "B" + i, ColumnC = "C" + i}).ToList();
DataContext = this;
}
public List<MyItem> Items { get; private set; }
private bool mShowColumnB;
public bool ShowColumnB
{
get { return mShowColumnB; }
set
{
if (mShowColumnB == value) return;
mShowColumnB = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ShowColumnB"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class DataGridContextHelper
{
static DataGridContextHelper()
{
FrameworkElement.DataContextProperty.OverrideMetadata(typeof(DataGrid),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataContextChanged));
}
public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as DataGrid;
if (grid == null) return;
foreach (var col in grid.Columns)
col.SetValue(FrameworkElement.DataContextProperty, e.NewValue);
}
}
[ValueConversion(typeof(bool), typeof(Visibility))]
public class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool && (bool)value)
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class MyItem
{
public string ColumnA { get; set; }
public string ColumnB { get; set; }
public string ColumnC { get; set; }
}
}
我創造我的解決方案來源this post by Jaime Rodriguez。
1
只要您的數據網格位於窗口,控件等中,只要它在控件模板中,這仍然不起作用
相關問題
- 1. DateTimePicker WPF工具包綁定
- 2. WPF工具提示可見性
- 3. WPF中DataGridColumn的綁定可見性
- 4. Wpf綁定到WindowsFormHost的可見性
- 5. WPF ContentPresenter未能綁定可見性
- 6. WPF通過綁定設置可見性
- 7. WPF工具提示數據綁定
- 8. WPF - 如何默認數據綁定文本塊的可見性?
- 9. wpf數據綁定網格行可見性
- 10. WPF DataGridTemplateColumn集具有綁定的可見性未觸發
- 11. 綁定集合WPF工具包圖表
- 12. MVVM綁定DataGrid(WPF工具包)到DataTable
- 13. WPF工具包MaskTextBox綁定問題
- 14. WPF工具包圖表控件綁定
- 15. AjaxControl工具包TabContainer可見性問題
- 16. 數據綁定與MVVM光工具包
- 17. knockoutjs - 數據綁定可見
- 18. WPF數據綁定性能
- 19. 將LegendItem複選框綁定到WPF中的系列可見性
- 20. wpf擴展工具包BusyIndicator綁定到屬性
- 21. 將模板列的可見性綁定到基礎數據
- 22. WPF在綁定數據包含列表時綁定GridView
- 23. 如何綁定到數據上下文WPF工具包數據網格
- 24. 數據綁定WPF工具包數據網格,以LINQ到SQL查詢
- 25. wpf工具包datagrid性能
- 26. WPF工具提示綁定
- 27. WPF工具包 - 代碼背後的綁定行系列
- 28. Silverlight DataGridTextColumn綁定可見性
- 29. Root MenuItem可見性綁定
- 30. 可見性綁定失敗