如何将复选框的IsChecked绑定到数据网格中的Expander IsExpanded [英] How to bind the IsChecked of a checkbox to Expander IsExpanded within a datagrid
问题描述
好的,我现在非常沮丧,请记住,我是WPF的新手,因此具有约束力..
我有一个
Datagrid(在tabcontrol中).在Datagrid中有一个扩展器.我在窗口顶部有一个复选框,称为chkExpandAll.我想将扩展器的IsExpanded绑定到复选框的IsChecked.换句话说,我希望在选中复选框时扩展数据网格中的所有扩展器.
在"Windows资源"部分中,我有:
Okay, I''m getting highly frustrated right now, and keep in mind that I am brand new to WPF and binding so..
I have a
Datagrid (that is within a tabcontrol). Within the Datagrid is an expander. I have a checkbox at the top of the Window called chkExpandAll. I want to bind the IsExpanded of the expander to the IsChecked of the checkbox. In other words I wish to expand all of the expanders in the datagrid when I select the checkbox.
In my Windows Resources section I have:
<local:BooleanToVisibilityConverter x:Key="boolConvertNormal" />
该复选框列为:
The checkbox is listed as:
<CheckBox Content="Expand All" Name="chkExpandAll" VerticalAlignment="Center" Margin="5,0,5,0" />
然后在我的扩展器上有这个:
And then on my expander I have this:
<Expander Header="{Binding}" IsExpanded="{Binding IsChecked, ElementName=chkExpandAll, Converter={StaticResource ResourceKey=boolConvertNormal}, Mode=TwoWay}" >
当我运行该应用程序,然后单击复选框时,没有任何反应.
哦,这是该资源的代码:
When I run the app and then click the checkbox nothing happens.
Oh, and here is the code for the resource:
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public bool IsReversed { get; set; }
public bool UseHidden { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
if (this.IsReversed)
{
val = !val;
}
if (val)
{
return Visibility.Visible;
}
return this.UseHidden ? Visibility.Hidden : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
推荐答案
好吧,我再次检查了复选框是否被选中,而对于我来说,我无法弄清为什么它不起作用.因此,我所做的是在我的视图模型上创建了一个名为Expanded的新属性,然后在复选框的Checked/Unchecked事件中,将视图模型的属性设置为true/false,然后将扩展器的IsExpanded绑定到viewmodel属性.
Well I double checked that the checkbox was getting checked, and for the life of me I could not figure out why it wasn''t working. So what I did instead was created a new property on my viewmodel called Expanded and then on the Checked/Unchecked events of the checkbox I set the property of the viewmodel''s to true/false, I then bound the IsExpanded of the expander to the viewmodel property.
<expander header="{Binding}" isexpanded="{Binding Path=isRowExpanded}"></expander>
public bool isRowExpanded
{
get;
set;
}
老实说,我不喜欢这种解决方案,而且我觉得它会变慢,因为我必须重申各行并更新该属性.但是它现在可以使用了,所以我就离开了.
Honestly I don''t like this solution and I feel it will be to slow as I have to reiterate over the rows and update the property. But it works for now so I''m leaving it.
这篇关于如何将复选框的IsChecked绑定到数据网格中的Expander IsExpanded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!