使按钮在 DataGrid 中可见
Posted
技术标签:
【中文标题】使按钮在 DataGrid 中可见【英文标题】:Making a Button Visible in DataGrid 【发布时间】:2012-08-12 16:48:16 【问题描述】:我在 WPF 中创建了一个 Datagrid 控件。我怎样才能使按钮仅对
可见我选择它的行,以便我的 Button 将显示在 Button-Column-Cell 中。
XAML:
<DataGridTemplateColumn x:Name="Button-Column" Header="H." Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataTemplate.Triggers>
<DataTrigger Binding="Binding Path=ObjectType" Value="E">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="Binding Path=ObjectType" Value="F">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="Binding Path=ObjectType" Value= "P">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</DataTemplate.Triggers>
<Button Name="btnTable" Visibility="Binding Path=ObjectType" Height="20"
Width="25" Click="Button_Table_Click">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
【问题讨论】:
【参考方案1】:这是使用多重绑定的解决方案:
xaml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication8="clr-namespace:WpfApplication8"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<WpfApplication8:ButtonVisibilityConverter x:Key="buttonVisibilityConverter"/>
</Window.Resources>
<DataGrid x:Name="dataGrid" SelectedItem="Binding MySelectedItem" ItemsSource="Binding MyObjectList" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTemplateColumn Header="H." Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Height="20"
Width="25">
<Button.Visibility>
<MultiBinding Converter="StaticResource buttonVisibilityConverter">
<Binding Path="Parent.MySelectedItem"/>
<Binding></Binding>
</MultiBinding>
</Button.Visibility>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
后面的代码:
namespace WpfApplication8
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public class Toto
public object Parent get; set;
public string Name get; set;
public string Content get; set;
public Toto(string name, string content, object parent)
this.Name = name; this.Content = content; this.Parent = parent;
private object _mySelectedItem;
public object MySelectedItem
get return _mySelectedItem;
set
_mySelectedItem = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("MySelectedItem"));
private List<Toto> _myObjectList;
public List<Toto> MyObjectList
get return _myObjectList;
set _myObjectList = value;
public MainWindow()
this.MyObjectList = new List<Toto>
new Toto("toto1", "content toto1", this),
new Toto("toto2", "content toto2", this)
;
InitializeComponent();
this.DataContext = this;
转换器:
class ButtonVisibilityConverter : IMultiValueConverter
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (values[0] == values[1])
return Visibility.Visible;
else
return Visibility.Hidden;
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
#endregion
【讨论】:
以上是关于使按钮在 DataGrid 中可见的主要内容,如果未能解决你的问题,请参考以下文章