C# 如何在自定义wpf控件上绑定datagrid列的可见性?
我花了一上午的时间看了相关的帖子,我发现没有一篇帖子能够解决我所遇到的问题,尽管我在这一过程中学到了更多 (在WPF中使用带有用户控件的MVVM) 场景:我需要创建一个可重用控件,它是一个datagrid,根据表单需求显示两列或三列。我有一个已经创建的自定义控件,以及一个用于隐藏/显示第三列选项的依赖项属性: *注意:此可见性完全取决于我将属性设置为什么,我从不需要根据其他区域的选择进行更改C# 如何在自定义wpf控件上绑定datagrid列的可见性?,c#,wpf,binding,visibility,C#,Wpf,Binding,Visibility,我花了一上午的时间看了相关的帖子,我发现没有一篇帖子能够解决我所遇到的问题,尽管我在这一过程中学到了更多 (在WPF中使用带有用户控件的MVVM) 场景:我需要创建一个可重用控件,它是一个datagrid,根据表单需求显示两列或三列。我有一个已经创建的自定义控件,以及一个用于隐藏/显示第三列选项的依赖项属性: *注意:此可见性完全取决于我将属性设置为什么,我从不需要根据其他区域的选择进行更改 public class MyCustomControl: Control { public s
public class MyCustomControl: Control
{
public static readonly DependencyProperty DisplayThirdColumnProperty = DependencyProperty.Register(
"DisplayThirdColumn",
typeof(bool),
typeof(MyCustomControl),
new FrameworkPropertyMetadata(false));
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
/// <summary>
/// Gets or sets a value indicating whether the the third column should display.
/// </summary>
public bool DisplayThirdColumn
{
get
{
return (bool)this.GetValue(DisplayThirdColumnProperty);
}
set
{
this.SetValue(DisplayThirdColumnProperty, value);
}
}
}
公共类MyCustomControl:控件
{
公共静态只读DependencyProperty DisplayThirdColumnProperty=DependencyProperty.Register(
“显示第三列”,
类型(bool),
类型(MyCustomControl),
新的FrameworkPropertyMetadata(假));
静态MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl)),new FrameworkPropertyMetadata(typeof(MyCustomControl));
}
///
///获取或设置一个值,该值指示是否应显示第三列。
///
公共布尔显示第三列
{
得到
{
返回(bool)this.GetValue(DisplayThirdColumnProperty);
}
设置
{
此.SetValue(DisplayThirdColumnProperty,value);
}
}
}
以下是xaml.Generic:
<CheckBoxColumn Binding="{Binding StuffInThirdColumn}"
Header="ThirdColumn"
Visibility="{Binding DisplayThirdColumn,
Converter={StaticResource BooleanToVisibilityConverter},RelativeSource={RelativeSource TemplatedParent}}"/>
现在,当我使用控件时:
<MyControls:MyCustomControl DisplayThirdColumn="False"/>
如果我的“新人”出现了,我很抱歉,但我是否遗漏了一些明显的东西?当我在控件xaml.Generic上将Visiblity属性设置为显式折叠时,它会正确地隐藏列:
<CheckBoxColumn Visibility="Collapsed"..../>
输出窗口似乎表明它找不到要应用它的元素
如果我不能使用相对源,你知道我可以用另一种方法来实现这一点吗
System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=DisplayThirdColumn;DataItem=null;目标元素是“CheckBoxColumn”(HashCode=19379515);目标属性为“可见性”(类型为“可见性”)
Visibility属性不接受“False”作为可能的值。如果要隐藏控件,则需要编写:
<CheckBoxColumn Visibility="Collapsed"/>
如果需要有关可见性属性及其所有可能值的详细信息,请转到此处:我将可见性属性绑定到ViewModel中的布尔值,并使用VisibilityConverter,请参阅
这意味着,如果我们绑定到的布尔属性为
true
,它将转换为可见性.Visible
,如果为false,则转换为可见性.Collapsed感谢大家的评论和输入,感谢大家花了一分钟的时间(我非常感谢你们抽出时间!)
以下是最终结果,如果其他人遇到这种情况,结果会如何:
帮助很大,但我需要的语法缺少TemplatedParent
的相关源代码:
(1) 我使用的是可消费控件,希望在实现该控件时能够设置此可见性。您可以使用上述文章中的步骤访问ViewModel上下文
(2) 您需要将绑定相对源放在代理或伪元素(即缺少的部分I)上的TemplatedParent
。。。在ControlTemplate中:
......
列不在同一可视树中,因此无法继承DataContext。你可以在这里寻找解决方案:更新原始帖子以删除Visibility=“False”
的打字错误。你还应该将你的答案标记为已接受的答案,以防其他人遇到你的问题。
<CheckBoxColumn Visibility="Hidden"/>
yourObject.Visibility = Visibility.Collapsed;
... In a ControlTemplate:
<FrameworkElement x:Name="dummyElementToGetDataContext"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Visibility="Collapsed" />
<DataGrid>
<DataGrid.Columns>
......
<CheckBoxColumn Binding="{Binding SecondColumnStuff}"
Visibility="{Binding DataContext.ShouldDisplaySecondColumn,
Converter={StaticResource BooleanToVisibilityConverter},
Source={x:Reference dummyElementToGetDataContext}}"
.............
<DataGrid.Resources>
<controls:ControlProxy x:Key="ControlProxy" Control="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</DataGrid.Resources>