XAML 代码
<TreeView.Resources>
<local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True"/>
</TreeView.Resources>
<TreeViewItem Header="First Child" Name="_firstChild"
Visibility="{Binding Path=VisibleOnCheck, Mode=OneWay, NotifyOnTargetUpdated=True, Converter={StaticResource BoolToVisConverter}}" />
<CheckBox Name="_checkBoxvisible" IsChecked="{Binding Path= VisibleOnCheck, Mode=TwoWay}" Content="show" Checked="CheckBox />
复选框已选中
private void CheckBox(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("", "", MessageBoxButton.YesNo) ==
System.Windows.Forms.DialogResult.Yes)
{
VisibleOnCheck = true;
}
else
{
VisibleOnCheck = false;
}
}
型号代码
Private bool __visible;
public bool VisibleOnCheck
{
get { return _ visible; }
set { _visible = value; OnPropertyChanged("VisibleOnCheck "); }
}
public class BoolToVisibleOrHidden : IValueConverter
{
#region Constructors
public BoolToVisibleOrHidden() { }
#endregion
#region Propertie Collapse
public bool Collapse { get; set; }
public bool Reverse { get; set; }
#endregion
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bValue = (bool)value;
if (bValue != Reverse)
{
return Visibility.Visible;
}
else
{
if (Collapse)
return Visibility.Collapsed;
else
return Visibility.Hidden;
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility) value;
if (visibility == Visibility.Visible)
return !Reverse;
else
return Reverse;
}
#endregion
}
我想要,当 MessageBox .Yes 单击时,Treeview 项目标题应该可见,但这里它的可见 MessageBox 和 TreeviewItem 同时不单击 Messagebox.yes,任何人都可以帮忙。