UserControl的多个实例没有绑定

时间:2015-03-01 13:48:31

标签: c# windows xaml user-controls windows-phone-8.1

您好我尝试使用TestValue1和TestValue2属性创建一个简单的UserControl,并在堆栈面板中创建它的两个实例。但是发生的事情是只有Last Control将值存储在User Control中,其余的只有空白值。

enter image description here

以下是使用它们的XAML代码:

<userControls:TestControl TestValue1="Vasu" TestValue2="Mahesh"></userControls:TestControl>
<userControls:TestControl TestValue1="Test" TestValue2="Testing2"></userControls:TestControl>

这是UserControl的XAML和Code Behind:

代码背后:

public sealed partial class TestControl : UserControl
    {
        public TestControl()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }

        public readonly DependencyProperty TestValue1DependencyProperty = DependencyProperty.Register("TestValue1", typeof(string), typeof(TestControl), new PropertyMetadata(default(string)));
        public string TestValue1
        {
            get
            {
                return (string)GetValue(TestValue1DependencyProperty);
            }
            set
            {
                SetValue(TestValue1DependencyProperty, value);
            }
        }

        public readonly DependencyProperty TestValue2DependencyProperty = DependencyProperty.Register("TestValue2", typeof(string), typeof(TestControl), new PropertyMetadata(default(string)));
        public string TestValue2
        {
            get
            {
                return (string)GetValue(TestValue2DependencyProperty);
            }
            set
            {
                SetValue(TestValue2DependencyProperty, value);
            }
        }
    }

XAML文件:

<UserControl
    x:Name="TestControlName"
    x:Class="downloader.UserControls.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:downloader.UserControls"
    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>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding TestValue1 , ElementName=TestControlName}"></TextBlock>
            <TextBlock Text="{Binding TestValue2 , ElementName=TestControlName}"></TextBlock>
        </StackPanel>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

解决!

Trick与DataContext一起使用:

在我设置的UserControl的XAML中

<UserControl
    x:Name="TestControlName"
    x:Class="downloader.UserControls.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:downloader.UserControls"
    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="LayoutRootGrid">
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding TestValue1}"></TextBlock>
            <TextBlock Text="{Binding TestValue2}"></TextBlock>
        </StackPanel>
    </Grid>
</UserControl>

在C#Code Behind:

//this.DataContext = this;
LayoutRootGrid.DataContext = this;

可能是因为this正在重写DataContext ...因为DataContext是从Parent继承的,所以通过使用旧行,它不再继承父类。

所以我给内部元素提供了代码的数据上下文,允许UserControl继承Parent的DataContext(即从它调用的地方)