简体   繁体   English

从另一个UserControl访问UserControl属性

[英]Access a UserControl property from another UserControl

I created a UserControl, which has a property called Hero 我创建了一个UserControl,它有一个名为Hero的属性

public partial class UcHeros : UserControl
{
    public UcHeros()
    {
        InitializeComponent();
        Hero = "Spiderman";
    }

    public static readonly DependencyProperty HeroProperty = DependencyProperty.Register("Hero", typeof(string), typeof(UcHeros), new PropertyMetadata(null));

    public string Hero
    {
        get { return (string)GetValue(HeroProperty); }
        set { SetValue(HeroProperty, value); }
    }
}

I'm using this UserControl inside a Window like this : 我在这样的Window中使用这个UserControl:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <wpfApplication1:UcHeros x:Name="Superhero" />    
            <Button Click="OnClick">Click</Button>
        </StackPanel>
    </Grid>
</Window>

Now to get the Hero value I use this : 现在要获得Hero值,我使用它:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty HumanProperty = DependencyProperty.Register("Human", typeof(string), typeof(MainWindow), new PropertyMetadata(null));

    public string Human
    {
        get { return (string)GetValue(HumanProperty); }
        set { SetValue(HumanProperty, value); }
    }

    private void OnClick(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine(Superhero.Hero); 
    }
}

I can access to the Hero because I gived a name to that UserControl in my XAML declaration x:Name="Superhero" , but how can I access to that value if I remove the Name property ? 我可以访问Hero,因为我在我的XAML声明x:Name="Superhero"给了UserControl一个名字,但是如果删除Name属性,如何访问该值?

I mean : How can I store the Hero value in the Human value using some sort of Binding ! 我的意思是:如何使用某种绑定将Hero值存储在Human值中!

Just Bind your Human property to the Hero property on your control: 只需将您的Human资源Bind到您控制的Hero财产:

<wpfApplication1:UcHeros Hero="{Binding Human, Mode=OneWayToSource}" />

Try using a OneWayToSource Binding if you just want to read the value and not update it. 如果您只想读取值而不更新它,请尝试使用OneWayToSource Binding


UPDATE >>> 更新>>>

As @Killercam suggested, try setting the default value for your property in the declaration instead of the constructor: 正如@Killercam建议的那样,尝试在声明中设置属性的默认值而不是构造函数:

public static readonly DependencyProperty HeroProperty = DependencyProperty.
    Register("Hero", typeof(string), typeof(UcHeros), 
    new PropertyMetadata("Spiderman"));

If that still doesn't work, then you've got something else going on there. 如果那仍然不起作用,那么你还有其他东西在那里。

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

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