[英]C# XAML Setter value to True depending on string property value
I am having trouble with what seems like a fairly straightforward task. 我在看似相当简单的任务上遇到了麻烦。 I have a treeview with nodes, and I would like to set the TreeViewItem 'IsExpanded' property to True for the whole treeview if a specific string property 'SearchTerm' of my View Model is not empty.
我有一个带有节点的树视图,如果我的视图模型的特定字符串属性“ SearchTerm”不为空,我想将整个树视图的TreeViewItem'IsExpanded'属性设置为True。 In other words, if string property is not null, IsExpanded value should be True.
换句话说,如果字符串属性不为null,则IsExpanded值应为True。 I have already done this in codebehind but I prefer to do this in XAML for cleanness.
我已经在代码隐藏中完成了此操作,但是为了简洁起见,我更喜欢在XAML中执行此操作。
To describe the code below, I created a converter which will convert a null string to 'False' and non-null to 'True'. 为了描述下面的代码,我创建了一个转换器,它将空字符串转换为“ False”,将非空字符串转换为“ True”。 In my XAML I call this converter when I attempt to bind the string value from the viewmodel in the TreeView ItemContainerStyle.
在我的XAML中,当我尝试从TreeView ItemContainerStyle中的viewmodel绑定字符串值时,我将此转换器称为。 It appears that the converter is never even fired.
看来转换器甚至从未被解雇。
My XAML (simplified): 我的XAML(简体):
<UserControl.Resources>
<cv:ExpandNodesIfSearchConverter x:Key="ExpandAll">
</cv:ExpandNodesIfSearchConverter>
</UserControl.Resources>
<TreeView Grid.Row="2" x:Name="myTreeView"
ItemsSource="{Binding Sponsors}"
SelectedItemChanged="TreeView_SelectedItemChanged" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<!-- if SearchTerm is not null, use converter to set value to true and expand all nodes -->
<Setter Property="IsExpanded" Value="{Binding Path=SearchTerm, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ExpandAll}}" />
</Style>
</TreeView.ItemContainerStyle>
<!-- TreeView data -->
</TreeView>
My View Model: 我的视图模型:
public class TreeViewVM : INotifyPropertyChanged
{
private string _searchterm;
public string SearchTerm
{
get
{
return _searchterm;
}
set
{
_searchterm = value;
OnPropertyChanged("SearchTerm");
}
}
}
My Converter: 我的转换器:
class ExpandNodesIfSearchConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//if searchterm is not null, return true to expand all items, otherwise return false
string searchterm = value.ToString();
if (string.IsNullOrEmpty(searchterm))
return false;
else
return true;
}
public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I solved my issue by using the ElementName in the Setter tag rather than the viewmodel property. 我通过在Setter标记中使用ElementName而不是viewmodel属性解决了我的问题。 searchTxt being the name of the TextBox which SeachTerm was bound to.
searchTxt是SeachTerm绑定到的TextBox的名称。
<Setter Property="IsExpanded" Value="{Binding ElementName=searchTxt, Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ExpandAll}}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.