WPF - 数据绑定 - 数据未显示在自定义用户控件中

问题描述 投票:0回答:1

我有一个用户控件,我尝试进行简单的绑定工作

无论我做什么,当我运行应用程序时,数据都不会显示在控件中

public partial class UserControlA: UserControl
{
    public static readonly DependencyProperty SomeDataProperty = DependencyProperty.Register("SomeData", typeof(int), typeof(UserControlA), new PropertyMetadata(10));

    public int SomeData
    {
        get {
            return (int) GetValue(SomeDataProperty ); 
        }
        set { 
            SetValue(SomeDataProperty , value); 
        }
    }

    public UserControlA()
    {
        InitializeComponent();

        SomeData = 5;
    }
}

控件的xaml看起来像这样

<UserControl x:Class="MyNameSpace.UserControlA"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
        
        <StackPanel Orientation="Vertical"  >

            <Label Content="{Binding Path=SomeData , RelativeSource={RelativeSource AncestorType=UserControl}}"/>
            <TextBlock Text="{Binding Path=SomeData , RelativeSource={RelativeSource AncestorType=UserControl}}"/>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=SomeData ,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
...

任何帮助表示赞赏

c# wpf data-binding user-controls
1个回答
0
投票

我的错,我已经设置了

Root="{Binding Root, Source={StaticResource SharedViewModelInstance}}"

在我的组件上

现在数据已正确显示

© www.soinside.com 2019 - 2024. All rights reserved.