[英]Binding BooleanToVisibilityConverter to a Control in a TreeView HierarchicalDataTemplate
I have a TreeView object bound to a DataSet. 我有一个绑定到DataSet的TreeView对象。 Inside of the TreeView.ItemTemplate, I am using a HierarchicalDataTemplate containing the controls that I am rendering. 在TreeView.ItemTemplate内部,我正在使用HierarchicalDataTemplate,其中包含要渲染的控件。
Does anyone know how to change the Visibility property of a control inside of a HierarchicalDataTemplate? 有谁知道如何在HierarchicalDataTemplate内部更改控件的Visibility属性? I have tried using the BooleanToVisibilityConverter from the .NET framework, but cannot get the binding to work properly. 我曾尝试使用.NET框架中的BooleanToVisibilityConverter,但无法使绑定正常工作。
The boolean variable in my ViewModel named " moveButtonVisibility " is bound to the Visibility property of the button in my XAML. 我的ViewModel中名为“ moveButtonVisibility ”的布尔变量绑定到XAML中按钮的Visibility属性。 The BooleanToVisibilityConverter then attempts to convert the corresponding boolean value (true/false) to a Visibility value (visible/hidden). 然后,BooleanToVisibilityConverter尝试将相应的布尔值(true / false)转换为Visibility值(visible / hidden)。 " moveButtonVisibility " is not part of the TreeView's ItemSource. “ moveButtonVisibility ”不是TreeView的ItemSource的一部分。
A stripped down version of my code is shown below. 我的代码的简化版本如下所示。 I have removed all of the code in my XAML except for the Button control "MoveHereButton" that I want to change the visibility property on: 我已删除了XAML中的所有代码,但我想更改其可见性属性的Button控件“ MoveHereButton”除外:
VIEWMODEL (C#): VIEWMODEL(C#):
private bool _moveButtonVisibility;
public bool moveButtonVisibility
{
get { return _moveButtonVisibility; }
set
{
_moveButtonVisibility = value;
RaiseChange("moveButtonVisibility");
}
}
VIEW (XAML): VIEW(XAML):
<Page>
<Page.Resources>
<BooleanToVisibilityConverter x:Key="visibilityConverter"/>
</Page.Resources>
<Grid HorizontalAlignment="Center" VerticalAlignment="Top">
<TreeView HorizontalAlignment="Center" x:Name="treeView1" VerticalAlignment="Top" ItemsSource="{Binding Path=rsParentChild}" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
<TreeView.ItemContainerStyle>
<Style>
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=rsParentChild, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Grid Focusable="False" Margin="5,10,5,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Name="MoveHereButton" Content="Move Here" Visibility="{Binding DataContext.moveButtonVisibility, Converter={StaticResource visibilityConverter}}" Click="MoveHereButton_Click" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Page>
The following worked: 以下工作:
<Button Name="MoveHereButton"
Content="Move Here"
Visibility="{Binding DataContext.moveButtonVisibility,
RelativeSource={RelativeSource AncestorType={x:Type Page}},
Converter={StaticResource visibilityConverter}}"
Click="MoveHereButton_Click" />
The key was to add: 关键是要添加:
RelativeSource={RelativeSource AncestorType={x:Type Page}}
inside of the Visibility
binding to force the control to use the DataContext
of the Page
. 在Visibility
绑定内部,以强制控件使用Page
的DataContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.