2016-09-16 1 views
0

J'essaie de créer un contrôle utilisateur avec une propriété de dépendance. J'ai besoin d'exécuter une certaine logique lorsque la propriété de dépendance est modifiée en dehors de la commande usercontrol, mais cette logique ne doit pas s'exécuter lorsque la propriété de dépendance est modifiée depuis l'intérieur du contrôle utilisateur. J'ai ce petit échantillon. Je veux seulement exécuter une certaine logique lorsque la valeur est définie à partir de mainwindow et non quand elle est définie en cliquant sur la case à cocher. Je ne sais pas si PropertyChangedCallback est la bonne façon, mais c'est ce que j'ai.La propriété de dépendance à sens unique a changé de notification

UserControl:

public partial class UserControl1 : UserControl 
{ 
    public int MyProperty 
    { 
     get { return (int)GetValue(MyPropertyProperty); } 
     set { SetValue(MyPropertyProperty, value); } 
    } 

    public static readonly DependencyProperty MyPropertyProperty = 
     DependencyProperty.Register("MyProperty", typeof(int), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged))); 

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     // Only process the 5, don't process the 6 
    } 


    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    private void checkBox_Click(object sender, RoutedEventArgs e) 
    { 
     MyProperty = 6; 
    } 
} 

UserControl xaml:

<UserControl x:Class="WpfApplication4.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <CheckBox x:Name="checkBox" Click="checkBox_Click"/> 
    </Grid> 
</UserControl> 

MainWindow:

public partial class MainWindow : Window 
    { 
     public int MainWindowProperty { get; set; } 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
      MainWindowProperty = 5; 
     } 
    } 

mainWindow xaml:

<Window x:Class="WpfApplication4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication4" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <local:UserControl1 MyProperty="{Binding MainWindowProperty}"/> 
    </Grid> 
</Window> 

Répondre

1
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    if (!disableProcessing) 
    { 
     // Only process the 5, don't process the 6 
    } 
} 

bool disableProcessing = false; 
private void checkBox_Click(object sender, RoutedEventArgs e) 
{ 
    disableProcessing = true; 
    MyProperty = 6; 
    disableProcessing = false; 
} 
+0

Vraiment, aussi simple que cela? Je me sens si stupide. Laissez-moi essayer cela dans ma vraie application avant de marquer cela comme une réponse. –