Wpf 如何通过绑定转换器引用TreeViewItem中的另一个控件?
我想根据同一个TreeViewItemContainer中组合框的SelectedItem绑定文本框的可见性。我想我可以使用转换器进行绑定,但我不知道如何将Wpf 如何通过绑定转换器引用TreeViewItem中的另一个控件?,wpf,binding,converter,Wpf,Binding,Converter,我想根据同一个TreeViewItemContainer中组合框的SelectedItem绑定文本框的可见性。我想我可以使用转换器进行绑定,但我不知道如何将组合框项作为文本框绑定的参数发送。这能做到吗 <TreeView> <TreeView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal">
组合框
项作为文本框
绑定的参数发送。这能做到吗
<TreeView>
<TreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ComboBox Margin="2,0" Name="SkillSelectCB" ItemsSource="{Binding PotentialChildren}" />
<TextBox Margin="2,0" Width="50" Visibility="{Binding ??}" />
<Button Margin="2,0" Content="Add" />
</StackPanel>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
这实际上是在一个HierarchycalDataTemplate中,上面的示例非常简单。“添加”
按钮
将根据在组合框
中选择的内容,为树视图
的视图模型添加新的子对象。可视性是文本框
将根据组合框
的SelectedItem的某些属性而改变。因此文本框的Xaml
:
<TextBox Margin="2,0"Width="50" Visibility="{Binding SelectedItem, ElementName=SkillSelectCB, Converter={StaticResource SkillToVisibilityConverter}}" />
你试了什么?你应该发布你的代码…@makc我不知道该尝试什么。我认为它可以用转换器来完成,但我似乎在任何地方都找不到一个例子。如果有帮助的话,我已经在
树视图中加入了Xaml。@makc Ah不管怎样,我得到了它,它比我想象的要容易,我昨晚一定太累了。我很高兴没有人帮忙:)
public class SkillToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = (Skill)value;
return (s == null || !s.Specialized) ? "Hidden" : "Visible";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}