2013-02-28 14 views
0

我想在同一TreeViewItemContainer中基於ComboBox的SelectedItem綁定TextBox的可見性。我想我可以使用轉換器進行綁定,但我不知道如何發送ComboBox項目作爲TextBox綁定的參數。這可以做到嗎?如何通過綁定轉換器引用TreeViewItem中的另一個控件?

<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> 

這實際上是在HierarchicalDataTemplate,上面的例子中是很小的。根據ComboBox中選擇的內容,「Add」Button將爲TreeView的ViewModel添加新的子項。可見性是TextBox將根據ComboBox的SelectedItem的某些屬性而變化。

+0

那你試試?你應該發佈你的代碼... – makc 2013-02-28 08:39:19

+0

@makc我不知道該怎麼嘗試。我認爲這可以用轉換器完成,但我似乎無法在任何地方找到示例。如果有幫助,我已將Xaml包含在「TreeView」中。 – 2013-02-28 16:11:31

+0

@makc啊,沒關係我知道了,這比我想象的容易,昨晚我一定累得太累了。 – 2013-02-28 16:29:43

回答

0

所以XAML中爲TextBox

<TextBox Margin="2,0"Width="50" Visibility="{Binding SelectedItem, ElementName=SkillSelectCB, Converter={StaticResource SkillToVisibilityConverter}}" /> 

和轉換器:

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(); 
    } 
} 
相關問題