2010-05-27 14 views
5

Tôi đã có một UserFrol WPF chứa DependencyProperty (MyProperty).Thay đổi giá trị ràng buộc, không ràng buộc chính nó

DependencyProperty được liên kết với Thuộc tính trong DataContext.

Bây giờ trong UserControl tôi muốn thay đổi giá trị của thuộc tính bị ràng buộc. Nhưng nếu tôi chỉ định MyProperty = NewValue, Ràng buộc bị mất và được thay thế bằng NewValue.

Điều tôi muốn đạt được là thay đổi thuộc tính DataContext mà DependencyProperty bị ràng buộc.

Làm cách nào để đạt được điều này thay vì thay đổi ràng buộc?

Để làm rõ: sử dụng một cái gì đó như MyTextBox.Text = "0"; Tôi sẽ giải phóng ràng buộc. Làm thế nào tôi có thể thiết lập văn bản, để nguyên ràng buộc để các tài sản văn bản là ràng buộc để sẽ thay đổi, quá.

Trả lời

3

Tôi không thể biết bạn đang làm gì sai mà không nhìn thấy mã của bạn. Dưới đây là một usercontrol đơn giản cho phép người dùng chọn màu.

<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 đã khắc phục được sự cố của tôi, cảm ơn !!! – Sam

1

Điều này sẽ hoạt động như vậy, vấn đề duy nhất là nguồn ràng buộc hộp văn bản nhất định sẽ được cập nhật khi hộp văn bản bị mất tiêu điểm. Đây là hành vi mặc định. Bạn có thể thay đổi nó bằng cách chỉ định UpdateSourceTrigger=PropertyChanged trong ràng buộc của bạn. Giống như vậy:

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

Bạn có thể sử dụng SetCurrentValue.

Phương thức SetCurrentValue thay đổi giá trị hiệu quả của thuộc tính, nhưng trình kích hoạt hiện tại, ràng buộc dữ liệu và kiểu sẽ tiếp tục hoạt động.

+1

Điều này đã thực hiện tương tự như gán trực tiếp cho Thuộc tính: Trên OneWay-binding nó đã loại bỏ ràng buộc, trên liên kết TwoWay, nó đã thay đổi thuộc tính bị ràng buộc. – Sam

+0

Wow. Cảm ơn bạn đã bình luận, Sam. Xoá bỏ một nhức đầu lớn của một vấn đề tôi đã có. Tôi đã không nhận ra rằng thiết lập giá trị ở phía bên đích của một ràng buộc OneWay xóa ràng buộc. Tôi đã sử dụng một sự kiện và chức năng Set trên đường dẫn nguồn ràng buộc và cố gắng sử dụng các ràng buộc chỉ trên đường trở về. Nó không hoạt động cho đến khi tôi đặt nó vào TwoWay. –

+0

Khi người ta không thể chỉ định một ràng buộc hai chiều về phong cách-Triggers, đây chỉ là thiên tài. Cảm ơn – LuckyLikey