你是对的。您将创建一个 viewmodel 变量来保存选定的列表框项。您还将创建另一个变量来保存文本块的可见性。然后,您可以从视图模型中设置文本块的可见性
private string _selectedListBoxItem;
private boolean _textBlockVisibility
public string SelectedListBoxItem
{
get {return _selectedListBoxItem;}
set{_selectedListBoxItem=value;
_textBlockVisibility=false;}
}
public Boolean TextBlockVisibilty
{
get{return _textBlockVisibility;};
set {_textBlockVisibility=value;};
}
您的 xaml 会将文本块的可见性绑定到 TextBlockVisibility。您将不得不使用可见性转换器。就像是:
public class BooleanVisibilityValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
if (((bool)value) == true)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}