2010-05-27 80 views
5

我有一个包含DependencyProperty(MyProperty)的WPF UserControl。更改绑定值,不绑定本身

DependencyProperty绑定到DataContext中的属性。

现在在用户控件中我想更改绑定属性的值。但是,如果我分配MyProperty = NewValue绑定丢失并由NewValue取代。

我想实现的是改变DataContext属性的DependencyProperty绑定。

如何实现这一点而不是更改绑定?

澄清:使用类似MyTextBox.Text = "0";的东西我会释放绑定。我将如何设置文本,保持绑定完好,所以文本绑定的属性也会改变。

回答

3

我看不出你的代码,你不知道你做错了什么。下面是一个简单的用户控件,允许用户选择一种颜色。

<UserControl x:Class="ColorPickerTest.ColorPicker" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <StackPanel Orientation="Horizontal"> 
     <ToggleButton Name="redButton" Content="Red" Click="Button_Click" /> 
     <ToggleButton Name="yellowButton" Content="Yellow" Click="Button_Click" /> 
    </StackPanel> 
</UserControl> 

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace ColorPickerTest 
{ 
    public partial class ColorPicker : UserControl 
    { 
     public ColorPicker() 
     { 
      InitializeComponent(); 
     } 

     public Brush SelectedColor 
     { 
      get { return (Brush)GetValue(SelectedColorProperty); } 
      set { SetValue(SelectedColorProperty, value); } 
     } 

     public static readonly DependencyProperty SelectedColorProperty = 
      DependencyProperty.Register("SelectedColor", 
             typeof(Brush), 
             typeof(ColorPicker), 
             new UIPropertyMetadata(Brushes.Transparent, OnPropertyChanged)); 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      if (!redButton.IsChecked.GetValueOrDefault() && !yellowButton.IsChecked.GetValueOrDefault()) 
      { 
       SelectedColor = Brushes.Transparent; 
      } 
      else if (!redButton.IsChecked.GetValueOrDefault() && yellowButton.IsChecked.GetValueOrDefault()) 
      { 
       SelectedColor = Brushes.Yellow; 
      } 
      else if (redButton.IsChecked.GetValueOrDefault() && !yellowButton.IsChecked.GetValueOrDefault()) 
      { 
       SelectedColor = Brushes.Red; 
      } 
      else 
      { 
       // redButton.IsChecked.GetValueOrDefault() && yellowButton.IsChecked.GetValueOrDefault()) 

       SelectedColor = Brushes.Orange; 
      } 
     } 

     private static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) 
     { 
      ColorPicker colorPicker = sender as ColorPicker; 
      colorPicker.redButton.IsChecked = colorPicker.SelectedColor == Brushes.Red || 
               colorPicker.SelectedColor == Brushes.Orange; 
      colorPicker.yellowButton.IsChecked = colorPicker.SelectedColor == Brushes.Yellow || 
               colorPicker.SelectedColor == Brushes.Orange; 
     } 
    } 
} 

<Window x:Class="ColorPickerTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ColorPickerTest="clr-namespace:ColorPickerTest" 
    Height="300" Width="300"> 
    <StackPanel> 
     <ColorPickerTest:ColorPicker SelectedColor="{Binding Path=MyColor, Mode=TwoWay}" /> 
    </StackPanel> 
</Window> 

using System.Windows; 
using System.Windows.Media; 

namespace ColorPickerTest 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      MyColor = Brushes.Red; 

      DataContext = this; 
     } 

     private Brush _myColor; 
     public Brush MyColor 
     { 
      get { return _myColor; } 
      set 
      { 
       _myColor = value; 
       Background = _myColor; 
      } 
     } 
    } 
} 
+1

Aahh,Mode = TwoWay修复了我的问题,谢谢! – Sam 2010-05-28 08:10:11

1

这应该是原样,唯一的问题是当文本框丢失焦点时,文本框绑定的绑定源将被更新。这是默认行为。您可以在绑定中指定UpdateSourceTrigger=PropertyChanged来更改它。像这样:

<TextBox Name="MyTextBox" Text="{Binding YourBindingPath, UpdateSourceTrigger=PropertyChanged}"/> 
4

您可以使用SetCurrentValue

SetCurrentValue方法更改属性的有效值,但现有的触发器,数据绑定和样式将继续工作。

+1

这与直接分配给属性的做法相同:在OneWay-binding上,它放弃了绑定,在TwoWay-binding上它改变了绑定属性。 – Sam 2010-05-28 08:44:05

+0

哇。谢谢你的评论,山姆。清理了我遇到的一个棘手的问题。我没有意识到设置OneWay绑定目标端的值会清除绑定。我在源绑定路径上使用了一个事件和Set函数,并试图仅在返回时使用绑定。直到我将它设置为TwoWay时,它才工作。 – 2010-07-29 14:40:27

+0

因为人们不能在样式触发器上指定双向绑定,这只是天才。谢谢 – LuckyLikey 2017-05-12 09:31:44