Associating TwoWay with UserControl

I am trying to set the twoway binding in the UserControl that I created.

When I use the control in Xaml, the DataContext is set like this ...

<uc:MyUserControl DataContext="{Binding Path=MyObject, Mode=TwoWay}" />

My user control is defined as follows.

<UserControl x:Class="SilverlightApplication1.XText"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="Text" Text="{Binding}"/>

    </Grid>
</UserControl>

The data is displayed correctly, however if I make this change, I would like it to be updated with TwoWay binding.

I tried this below, but these are runtime errors since Path is not defined.

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="Text" Text="{Binding Mode=TwoWay}"/>

    </Grid>
</UserControl>

Any ideas on how to get control inside usercontrol for twoway are tied to a DataContext?

+3
source share
3 answers

, , . UserControl, , , .

. : UserControl name, TextBox , XAML

XAML

<UserControl 
    x:Class="SilverlightApplication1.XText"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" 
    d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="MyText"/>    
    </Grid>
</UserControl>

CodeBehind

public partial class XText
{
    public XText()
    {
       InitializeComponent();
       MyText.SetBinding(TextBox.TextProperty, new Binding() 
       { 
          Source = this,  
          Path = new PropertyPath("Value"), 
          Mode = BindingMode.TwoWay
       });
    }


    public static DependencyProperty ValueProperty =
        DependencyProperty.Register(
            "Value",
            typeof(string),
            typeof(XText),
            new PropertyMetadata(null)
        );

    public string Value
    {
        get { return ((string)(GetValue(ValueProperty))); }
        set { SetValue(ValueProperty, value); }
    }

    ...
}

,

<uc:XText Value="{Binding Path=MyObject, Mode=TwoWay}" />
+1

(self-answer), , , , . , , , , , .

:

<UserControl 
    x:Class="SilverlightApplication1.XText"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Name="UserControl"
    d:DesignHeight="300" 
    d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="Text" Text="{Binding Path=Value, ElementName=UserControl, Mode=TwoWay}"/>    
    </Grid>
</UserControl>

codebehind:

public partial class XText
{
    public static DependencyProperty ValueProperty =
        DependencyProperty.Register(
            "Value",
            typeof(string),
            typeof(XText),
            new FrameworkPropertyMetadata(null)
        );

    public string Value
    {
        get { return ((string)(base.GetValue(XText.ValueProperty))); }
        set { base.SetValue(XText.ValueProperty, value); }
    }

    ...
}

, :

<uc:XText Value="{Binding Path=MyObject, Mode=TwoWay}" />

, , , Value UserControl, .

?

-Doug

: .

+2

, , .

UserControl ...

public Binding BindingValue
{
   set { this.MyTextBox.SetBinding(TextBox.TextProperty, value); }
}

XAML

<uc:MyUserControl BindingValue="{Binding Path=MyObject, Mode=TwoWay}" />
0

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


All Articles