简体   繁体   English

C#uwp中的双重绑定

[英]Double Binding in c# uwp

I have a problem. 我有个问题。 I created 2 controls for my application: a BottomMenu control and ResultBox. 我为应用程序创建了2个控件:BottomMenu控件和ResultBox。 ResultBox is contained in BottomMenu so the order goes like this: Page -> BottomMenu -> ResultBox. ResultBox包含在BottomMenu中,因此顺序如下:页面-> BottomMenu-> ResultBox。 I created a dependency property in ResultBox called Result of type string. 我在ResultBox中创建了一个依赖属性,称为字符串类型的Result。

    public ResultBox()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    /// <summary>
    /// Property used to store the result of the calculation
    /// </summary>
    public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
        "Result",
        typeof(string),
        typeof(ResultBox),
        new PropertyMetadata(null)
        );

    /// <summary>
    /// String holding the text assigned to the Result
    /// </summary>
    public string Result
    {
        get => (string)GetValue(ResultProperty);
        set => SetValue(ResultProperty, value);
    }

The binding is as follows: 绑定如下:

<TextBlock  Style="{StaticResource DefaultTextBlockStyle}"
                        Text="{Binding Result}"/>

Then I created identical dependency property in the BottomMenu so thatcan set it directly from the page. 然后,我在BottomMenu中创建了相同的依赖项属性,以便可以直接从页面进行设置。

    public BottomMenu()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
    /// <summary>
    /// Property used to store the result of the calculation
    /// </summary>
    public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
        "Result",
        typeof(string),
        typeof(BottomMenu),
        new PropertyMetadata(null)
        );

    /// <summary>
    /// String holding the text assigned to the Result
    /// </summary>
    public string Result
    {
        get => (string)GetValue(ResultProperty);
        set => SetValue(ResultProperty, value);
    }

And binding: 并绑定:

<local:ResultBox Grid.Row="1" Margin="0 0 0 10"
                        Result="{Binding Result}"
                        />

Unfortunately, the text only shows up if I directly input it in the declaration of ResultBox. 不幸的是,仅当我在ResultBox的声明中直接输入文本时,该文本才会显示。 When I do the double binding and input it in the page, 当我进行双重装订并将其输入页面时,

<local:BottomMenu Grid.Row="2"                              
                            Result="13"/>

it doesn't work. 它不起作用。 I'm learning binding and I'd like to know where I'm doing it wrong, or if it's even the proper way to do this thing. 我正在学习绑定,我想知道我在哪里做错了,或者这甚至是做这件事的正确方法。

Edit: Binding in ResultBox should't contain Source, fixed now. 编辑:ResultBox中的绑定不应包含Source,现在已修复。

You should almost never use this.DataContext = this ; 您几乎应该永远不要使用this.DataContext = this ; inside controls. 内部控件。 Always define the DataContext at the top level page and let it flow down to each control along the way. 始终在顶层页面上定义DataContext ,并使其一直沿流程流向每个控件。

So first remove 所以先删除

this.DataContext = this;

You have defined the dependency property Result correctly. 您已经正确定义了依赖项属性Result To bind the TextBlock 's Text to it, you can name your control and use ElementName binding, or more efficiently (UWP only), use x:Bind like the following 要将TextBlockText绑定到它,可以命名控件并使用ElementName绑定,或者更有效地(仅UWP)使用x:Bind ,如下所示

<TextBlock Style="{StaticResource DefaultTextBlockStyle}" Text="{x:Bind Result, Mode=OneWay}"/>

Same goes to this 一样

<local:ResultBox Grid.Row="1" Margin="0 0 0 10" Result="{x:Bind Result, Mode=OneWay}" />

Hope this helps. 希望这可以帮助。

Here, instead of setting this.DataContext = this; 在这里,而不是设置this.DataContext = this; , we should use (this.Content as FrameworkElement).DataContext = this; ,我们应该使用(this.Content as FrameworkElement).DataContext = this; like the following: 如下所示:

public BottomMenu()
{
    this.InitializeComponent();
    (this.Content as FrameworkElement).DataContext = this;
}

public ResultBox()
{
    this.InitializeComponent();
    (this.Content as FrameworkElement).DataContext = this; 
}

In the code above we don't set the data context of the user control, we set the data context of the first child in the user control. 在上面的代码中,我们没有设置用户控件的数据上下文,而是在用户控件中设置了第一个孩子的数据上下文。 After this, the ResultBox inside BottomMenu can inherit down the data context from BottomMenu and its Result property can be set properly. 在此之后, ResultBoxBottomMenu可以继承下来从数据上下文BottomMenuResult属性可以适当地设定。 For more info, please see Jerry Nixon's blog: Walkthrough: Two-way binding inside a XAML User Control . 有关更多信息,请参见Jerry Nixon的博客: 演练:XAML用户控件内的双向绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM