C# UWP:将值从UserControl传递到MainPage

C# UWP:将值从UserControl传递到MainPage,c#,xaml,user-controls,uwp,C#,Xaml,User Controls,Uwp,我已经创建了一个UserControl,其中的所有操作都在UserControl.xaml.cs中完成,我希望将UserControl中的一个特定属性(称为“Value”)传递给在MainPage中创建的TextBlock。为了测试对属性的访问,我在UserControl中创建了一个TextBlock,并通过Text={Binding Path=Value}将文本绑定到“Value”,效果很好。如何从主页绑定TextBlock以实现相同的功能 您可能可以使用绑定的ElementName部分从Us

我已经创建了一个UserControl,其中的所有操作都在UserControl.xaml.cs中完成,我希望将UserControl中的一个特定属性(称为“Value”)传递给在MainPage中创建的TextBlock。为了测试对属性的访问,我在UserControl中创建了一个TextBlock,并通过
Text={Binding Path=Value}
将文本绑定到“Value”,效果很好。如何从主页绑定TextBlock以实现相同的功能

您可能可以使用绑定的
ElementName
部分从UserControl访问值。要做到这一点,您必须为UserControl指定一个
x:Name
,然后按如下方式设置绑定:

Text="{Binding Value, ElementName=MyUserControl}"

您可能可以使用绑定的
ElementName
部分从UserControl访问值。要做到这一点,您必须为UserControl指定一个
x:Name
,然后按如下方式设置绑定:

Text="{Binding Value, ElementName=MyUserControl}"

确保已将属性创建为从属属性。您可以使用下面的代码来完成

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

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value", typeof(string), typeof(UserControl ), new PropertyMetadata(""));
<TextBlock Text="{Binding ElementName=UserControl, Path=Value}"/>
您可以使用下面的代码获得XAML中的值

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

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value", typeof(string), typeof(UserControl ), new PropertyMetadata(""));
<TextBlock Text="{Binding ElementName=UserControl, Path=Value}"/>

(或)



注意:使用
x:Bind
,因为它比
绑定
更有效

请确保已将属性创建为从属属性。您可以使用下面的代码来完成

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

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value", typeof(string), typeof(UserControl ), new PropertyMetadata(""));
<TextBlock Text="{Binding ElementName=UserControl, Path=Value}"/>
您可以使用下面的代码获得XAML中的值

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

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value", typeof(string), typeof(UserControl ), new PropertyMetadata(""));
<TextBlock Text="{Binding ElementName=UserControl, Path=Value}"/>

(或)



注意:使用
x:Bind
,因为它比
绑定

更有效。是否要将
用户控件
中的
属性绑定到主页面中的
文本块
?如果是,那么使用
Text={x:Bind UserControl.Value}
似乎就是这样的方法,但似乎我在某个地方丢失了数据上下文,因为我遇到了以下错误:`无法解析类型为'MyProject.TestApplication.MainPage'的数据上下文中的字段或属性'UserControl',您是否将
创建为从属属性?是否要将
UserControl
中的
属性绑定到主页中的
TextBlock
?如果是,则使用
Text={x:Bind UserControl.Value}
似乎就是这样,但似乎我在某个地方缺少数据上下文,因为我遇到了以下错误`无法解析'MyProject.TestApplication.MainPage'类型的数据上下文中的字段或属性'UserControl'。是否将
Value
创建为从属属性?是,虽然我一直将我的财产作为从属财产,但我缺少元素名称。谢谢你和@jsmyth886@Nigel Lee您还可以使用
x:Bind
并设置
Mode=OneWay
。我已经更新了答案。是的,虽然我一直将我的属性作为从属属性,但我缺少元素名称。谢谢你和@jsmyth886@Nigel Lee您还可以使用
x:Bind
并设置
Mode=OneWay
。我已经更新了答案。