更改绑定值,而不是本身的结合 [英] Change binding value, not binding itself
问题描述
我有含的DependencyProperty(myProperty的)。一个WPF用户控件
I've got a WPF UserControl containing a DependencyProperty (MyProperty).
将DependencyProperty绑定到DataContext的一个属性。
The DependencyProperty is bound to a Property in the DataContext.
现在在该用户我想改变绑定属性的值。但是,如果我给 = myProperty的的NewValue
绑定被丢失的NewValue和更换。
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.
我想实现的就是改变的DataContext属性将DependencyProperty势必。
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?
要澄清:使用类似 MyTextBox.Text =0;
我要解除绑定。我将如何设置文本,保留完好的结合使该属性的文本,势必会改变了。
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.
推荐答案
我不能告诉你,没有看到你的code做错了什么。下面是一个简单的用户控件,它允许用户选择一种颜色。
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;
}
}
}
}
这篇关于更改绑定值,而不是本身的结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!