[英]IConverter C#,wpf ,checkbox.Checked
XAML Code 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 />
CheckBox Checked 复选框已选中
private void CheckBox(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("", "", MessageBoxButton.YesNo) ==
System.Windows.Forms.DialogResult.Yes)
{
VisibleOnCheck = true;
}
else
{
VisibleOnCheck = false;
}
}
MODEL CODE 型号代码
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
}
I want ,Treeview item header should visible when MessageBox .Yes clicked, but here its visible's MessageBox and TreeviewItem same time without clicking Messagebox.yes, can any one help please. 我希望在单击MessageBox时,Treeview项标题应该可见。是的,但是在不单击Messagebox.yes的情况下,其可见的MessageBox和TreeviewItem可以同时显示,请问有谁可以帮忙。
You have a TwoWay binding on the CheckBox to VisibleOnCheck
, so if you check the Checkbox
its going to toggle the Visibility
on the TreeViewItem
. 您在CheckBox上具有到VisibleOnCheck
的TwoWay绑定,因此,如果选中Checkbox
它将在TreeViewItem
上切换Visibility
。
If you want to handle the VisibleOnCheck
in the Checked event handler based on the DialogResult
you will have to remove the TwoWay binding or the binding altogether. 如果要基于DialogResult
在Checked事件处理程序中处理VisibleOnCheck
,则必须删除TwoWay绑定或完全删除该绑定。
<CheckBox Name="_checkBoxvisible" Content="show" Checked="CheckBox />
I would also suggest using the WPF
MessageBox
over the Winforms
one, seems a bit odd to drag in another framework for a MessageBox
. 我还建议在Winforms
使用WPF
MessageBox
,将其拖到MessageBox
另一个框架中似乎有些奇怪。
private void CheckBox(object sender, RoutedEventArgs e)
{
VisibleOnCheck = MessageBox.Show("","", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
}
Since the problem is due to both controls binding to VisibleOnCheck
you could just bind the TreeViewItem
Visibility
directly to the Checkbox
in the Xaml
由于问题是由于两个控件都绑定到VisibleOnCheck
, VisibleOnCheck
您可以将TreeViewItem
Visibility
直接绑定到Xaml
的Checkbox
。
<TextBlock Text="TreeViewItem" Visibility="{Binding IsChecked, ElementName=chkbx, Converter={StaticResource BooleanToVisibilityConverter}}" />
<CheckBox x:Name="chkbx" Content="CheckBox" Checked="Checked" />
and in the event just set the IsChecked
based on your Dialog result 并根据对话框结果设置IsChecked
private void Checked(object sender, RoutedEventArgs e)
{
(sender as CheckBox).IsChecked = MessageBox.Show("","", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.