我有以下.xaml for DataGrid,并希望在.cs代码中的某些条件下显示/隐藏按钮.xaml代码如下
<DataGridTemplateColumn Header="Action" Width="auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnConfirm" Content="Confirm" Click="ConfirmButton_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/>
<Button x:Name="btnDecline" Content="Decline" Click="btnDecline_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" />
<Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
和.cs代码为
foreach (sp_ProcessingJobsResult item in grdUnConfirmJobs.ItemsSource)
{
var row = grdUnConfirmJobs.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow;
if (item.Status == "Cancellation Requested.")
{
//how find control
}
}
答案 0 :(得分:0)
你必须为此使用Binding。所以,让我们采取快速的方法:
以下是此背后的代码:
<svg width="450" height="450">
<defs>
<pattern id="img1" width="100%" height="100%">
<image xlink:href="http://lorempixel.com/200/200/people/" x="0" y="0" width="200" height="200" />
</pattern>
<pattern id="img2" width="100%" height="100%">
<image xlink:href="http://lorempixel.com/200/200/animals/" x="0" y="0" width="200" height="200" />
</pattern>
<pattern id="img3" width="100%" height="100%">
<image xlink:href="http://lorempixel.com/200/200/abstract/" x="0" y="0" width="200" height="200" />
</pattern>
<pattern id="img4" width="100%" height="100%">
<image xlink:href="http://lorempixel.com/200/200/sports/" x="0" y="0" width="200" height="200"/>
</pattern>
</defs>
<polygon id="path1" points="225,450 300,300 150,300" fill="url(#img1)" />
<polygon id="path2" points="150,300 225,150 75,150" fill="url(#img2)" />
<polygon id="path3" points="150,300 300,300 225,150" fill="url(#img3)" />
<polygon id="path4" points="300,300 375,150 225,150" fill="url(#img4)" />
<polygon id="path5" points="75,150 150,0 0,0" fill="url(#img1)" />
<polygon id="path6" points="75,150 225,150 150,0" fill="url(#img2)" />
<polygon id="path7" points="225,150 300,0 150,0" fill="url(#img3)" />
<polygon id="path8" points="225,150 375,150 300,0" fill="url(#img4)" />
<polygon id="path9" points="375,150 450,0 300,0" fill="url(#img1)" />
</svg>
这是我的例子的模型:
public partial class MainWindow : Window
{
public bool ButtonIsVisible
{
get { return (bool)GetValue(ButtonIsVisibleProperty); }
set { SetValue(ButtonIsVisibleProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonIsVisible. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonIsVisibleProperty =
DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));
public ObservableCollection<Person> items { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<Person>();
items.Add(new Person() { Name = "FirstName" });
items.Add(new Person() { Name = "SecondName" });
this.DataContext = this;
}
}
您的可见性不是bool类型,因此我们需要一个转换器:
public class Person : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
以下是整个XAML代码:
public class BoolToVis : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isVisible = (bool)value;
return isVisible ? Visibility.Visible : Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
实际上,你会在Buttons Visibility中看到一个奇怪的Binding语法。这是为什么?我假设你不需要在模型中使用这个功能,所以我回到了DataGrid的DataContext,以便达到DependencyProperty。我不完全确定你的情况,但我已经向你展示了一些易于使用的机制。