更改绑定值,而不是绑定本身

发布于 2024-09-03 01:15:00 字数 398 浏览 3 评论 0原文

我有一个包含 DependencyProperty (MyProperty) 的 WPF UserControl。

DependencyProperty 绑定到 DataContext 中的属性。

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

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

我如何实现这一目标而不是更改绑定?

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

I've got a WPF UserControl containing a DependencyProperty (MyProperty).

The DependencyProperty is bound to a Property in the DataContext.

Now in the UserControl I want to change the value of the bound property. But if I assign MyProperty = NewValue the Binding is lost and replaced by NewValue.

What I want to achieve is change the DataContext-property the DependencyProperty is bound to.

How do I achieve this instead of changing the binding?

To clarify: using something like MyTextBox.Text = "0"; I'll release the binding. How would I set Text, leave the binding intact so the property Text is bound to will change, too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2024-09-10 01:15:00

您可以使用SetCurrentValue

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

You can use SetCurrentValue.

The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.

神魇的王 2024-09-10 01:15:00

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

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

I can't tell what you are doing wrong without seeing your code. Below is a simple usercontrol that allows users to pick a color.

<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;
            }
        }
    }
}
_畞蕅 2024-09-10 01:15:00

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

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

This should work as is, the only problem is that binding source the text box is bound to will be updated when the text box loses focus. This is the default behavior. You can change it by specifying UpdateSourceTrigger=PropertyChanged within your binding. Like so:

<TextBox Name="MyTextBox" Text="{Binding YourBindingPath, UpdateSourceTrigger=PropertyChanged}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文