2017-07-03 2 views
0

Comment écrire une liaison pour "IsChecked"? Je veux vérifier l'un des 2 menus.WPF ContextMenu IsChecked Liaison

Si je clique sur "Hello1", je veux ajouter la vérification sur "Hello1", et supprimer la vérification de "Hello2".

Si je clique sur "Hello2", je veux ajouter la vérification sur "Hello2", et supprimer la vérification de "Hello1".

J'ai essayé pendant 1 semaine. mais je ne peux pas. S'il vous plaît aidez-moi "Comment écrire la liaison" Il n'y a pas d'exemple pour internet. J'ai récemment commencé la programmation WPF. Je ne peux que faire une demande de formulaire. il est très grand différent ....

<Window x:Class="WpfApp2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApp2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <ContextMenu Name="ContextMenuMain" x:Key="menuListBox"> 
      <MenuItem Header="Hello1" IsChecked="{Binding Path=IsCheck1, Mode=OneWay}" Name="ContextMenu_Hello1" Click="ContextMenuClick_Hello1"/> 
     <MenuItem Header="Hello2" IsChecked="{Binding Path=IsCheck2, Mode=OneWay}" Name="ContextMenu_Hello2" Click="ContextMenuClick_Hello2"/> 
    </ContextMenu> 
    </Window.Resources> 

</Window> 

Mon point le plus difficile est inférieur à code. J'ai essayé d'utiliser le nom de "ContextMenu_Hello1" dans MainWindow. mais C# ne me permet pas de l'utiliser ...

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      ContextMenu_Hello1.DataContext // error ! 
     } 
    } 

Est-ce hors de portée? mais pourquoi ?

Répondre

1

Définissez la propriété ContextMenu de la fenêtre. Vous devez également définir la propriété IsCheckable sur true si vous voulez être en mesure de vérifier les éléments de menu. , Vous ne pouvez pas paramétrer le mode de liaison à OneWay:

<Window x:Class="WpfApp2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApp2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.ContextMenu> 
     <ContextMenu Name="ContextMenuMain"> 
      <MenuItem Header="Hello1" IsCheckable="True" IsChecked="{Binding Path=IsCheck1}" Name="ContextMenu_Hello1" /> 
      <MenuItem Header="Hello2" IsCheckable="True" IsChecked="{Binding Path=IsCheck2}" Name="ContextMenu_Hello2" /> 
     </ContextMenu> 
    </Window.ContextMenu> 
</Window> 

Quand vous avez fait ces changements, vous pouvez lier à une propriété du DataContext:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    private bool _isCheck1; 
    public bool IsCheck1 
    { 
     get { return _isCheck1; } 
     set 
     { 
      _isCheck1 = value; 
      NotifyPropertyChanged(); 
      _isCheck2 = !_isCheck1; 
      NotifyPropertyChanged("IsCheck2"); 
     } 
    } 

    private bool _isCheck2; 
    public bool IsCheck2 
    { 
     get { return _isCheck2; } 
     set 
     { 
      _isCheck2 = value; 
      NotifyPropertyChanged(); 
      _isCheck1 = !_isCheck2; 
      NotifyPropertyChanged("IsCheck1"); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

ça marche! Je vous remercie ! :) –