How to set the value of a dependency property?

Please use the code below for context.

  • When you run the text 2 text fields will be "This is the original value."
  • When the button is pressed TestBox("Test" button):
    • the text TestBox TextBoxwill change to "Set by test button"
    • the other value of the TextBox will not change.
  • When the button is pressed Window, the text of the BOTH TextBoxes should change to "Set to Window." However, only the regular TextBox TestBoxis updated, no. <- THIS IS AN ERROR!

It seems that the way that I (re) set the property Testfrom TestBoxerases the binding.

What is the correct way to change the dependency property from the user control itself without breaking the bindings?


Code example:

I have a UserControl TestBox, which looks like this:

TestBox.xaml:

<UserControl x:Class="Company.UserControls.TestBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="TextBoxControl">
    <StackPanel>
        <TextBox MinWidth="100" Name="TestTextBox" 
            Text="{Binding Path=Test, ElementName=TextBoxControl, Mode=TwoWay}" 
         />
        <Button MinWidth="100" Content="Test Button" 
            Click="ButtonBase_OnClick" />
    </StackPanel>
</UserControl>

TestBox.xaml.cs:

using System.Windows;

namespace Company.UserControls
{
    public partial class TestBox
    {
        public const string TestString = "Set By Test Button";

        public TestBox()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty TestProperty = 
                DependencyProperty.Register(
                    "Test", 
                    typeof(string), typeof(TestBox),
            new FrameworkPropertyMetadata(null, 
                        FrameworkPropertyMetadataOptions.AffectsRender));
        public string Test
        {
            get { return (string)GetValue(TestProperty); }
            set { SetValue(TestProperty, value); }
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            /****** THIS OBLITERATES THE BINDING ******/
        Test = TestString;
            /****** THIS OBLITERATES THE BINDING ******/
        }
    }
}

And a window that uses the control as follows:

MainWindow.xaml:

<Window x:Class="Company.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:u="clr-namespace:Company.UserControls"
        Title="MainWindow">
    <StackPanel x:Name="MyStackPanel">
        <TextBox Text="{Binding Path=MyTestValue, Mode=OneWay}"/>
        <u:TestBox x:Name="MyTestBox" 
                   Test="{Binding Path=MyTestValue, Mode=OneWay}"/>
        <Button Content="Click" Click="ButtonBase_OnClick" />
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System.Windows;

namespace Company
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            MyStackPanel.DataContext = new MyThing
            {
                MyTestValue = "This is the Original Value"
            };
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            MyStackPanel.DataContext = new MyThing
            {
                MyTestValue = "Set by Window"
            };
        }
    }

    public class MyThing
    {
        public string MyTestValue { get; set; }
    }
}
+4
source share
1 answer

, . . , , , - "TwoWay" "OneWayToSource". , , , . .

TestBox. , . ( FrameworkPropertyMetadata). .

UserControl , ( ) : Name.

, .

0

Source: https://habr.com/ru/post/1545761/


All Articles