【问题标题】:Change binding value, not binding itself更改绑定值,而不是绑定本身
【发布时间】:2011-02-24 16:50:30
【问题描述】:

我有一个 WPF 用户控件,其中包含一个 DependencyProperty (MyProperty)。

DependencyProperty 绑定到 DataContext 中的属性。

现在我想在 UserControl 中更改绑定属性的值。但是如果我分配MyProperty = NewValue,Binding 就会丢失并被 NewValue 替换。

我想要实现的是更改 DependencyProperty 绑定到的 DataContext 属性。

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

澄清一下:使用MyTextBox.Text = "0"; 之类的东西我将释放绑定。我将如何设置 Text,保持绑定不变,因此 Text 绑定的属性也会改变。

【问题讨论】:

    标签: c# wpf binding dependency-properties


    【解决方案1】:

    您可以使用SetCurrentValue

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

    【讨论】:

    • 这与直接分配给属性的作用相同:在 OneWay-binding 上它删除了绑定,在 TwoWay-binding 上它更改了绑定属性。
    • 哇。谢谢你的评论,山姆。解决了我遇到的一个问题。我没有意识到在 OneWay 绑定的目标端设置值会清除绑定。我在源绑定路径上使用了事件和 Set 函数,并尝试仅在返回的路上使用绑定。在我将其设置为 TwoWay 之前它无法正常工作。
    • 由于无法在样式触发器上指定双向绑定,这简直是天才。谢谢
    【解决方案2】:

    如果没有看到您的代码,我无法判断您做错了什么。下面是一个简单的用户控件,允许用户选择颜色。

    <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;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这应该可以正常工作,唯一的问题是文本框绑定到的绑定源将在文本框失去焦点时更新。这是默认行为。您可以通过在绑定中指定 UpdateSourceTrigger=PropertyChanged 来更改它。像这样:

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

      【讨论】:

        猜你喜欢
        • 2017-12-08
        • 1970-01-01
        • 2017-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 2019-01-03
        • 2019-11-02
        相关资源
        最近更新 更多