IConverter C#,wpf,checkbox.Checked

时间:2013-05-03 09:14:37

标签: c# wpf visual-studio-2010

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

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,任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

您在CheckBox上有VisibleOnCheck的TwoWay绑定,因此如果您检查Checkbox它将切换Visibility上的TreeViewItem

如果要根据VisibleOnCheck处理Checked事件处理程序中的DialogResult,则必须完全删除TwoWay绑定或绑定。

<CheckBox Name="_checkBoxvisible" Content="show" Checked="CheckBox />

我还建议在WPF上使用MessageBox Winforms,在MessageBox的另一个框架中拖动似乎有点奇怪。

private void CheckBox(object sender, RoutedEventArgs e)
{
   VisibleOnCheck = MessageBox.Show("","", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
} 

修改

由于问题是由于两个控件绑定到VisibleOnCheck,您只需将TreeViewItem Visibility直接绑定到Checkbox中的Xaml

<TextBlock Text="TreeViewItem" Visibility="{Binding IsChecked, ElementName=chkbx, Converter={StaticResource BooleanToVisibilityConverter}}" />
<CheckBox x:Name="chkbx" Content="CheckBox" Checked="Checked"   />

并且根据您的对话结果设置IsChecked

private void Checked(object sender, RoutedEventArgs e)
{
     (sender as CheckBox).IsChecked = MessageBox.Show("","", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
}