繁体   English   中英

在另一个UserControl中时,通用App XAML绑定在UserControl上不起作用

[英]Universal App XAML binding not working on UserControl when inside another UserControl

我目前正在使用Windows通用应用程序。 总的来说,我对XAML还是很陌生,但是有一些经验。

我遇到的问题是围绕UserControl内部的绑定。 我环顾四周,无法找到特定问题的答案。

我有一个链接到ViewModel的XAML页面,一切正常。 然后在该页面上,我使用的UserControl基本上只是一个面板,其标题包含一些内容。 在该面板的内容中,我有另一个UserControl,它基本上仅由Label和TextBox组成。

当我将ViewModel的内容绑定到ContentPanel UserControl时,一切工作正常,它将拾取ViewModel上下文并正确绑定。

但是,当我尝试绑定到ContentPanel包含的LabelledTextbox UserControl时,绑定失败,因为它只是在ContentPanel上查找ViewModel上的属性。

请参阅下面的代码

Page.xaml

<!--Page.xaml-->                    
<cc:ContentPanel PanelHeading="LEFT FOOT: Measurements" PanelHeadingBackground="{StaticResource OPCare.PanelHeader}">
                    <StackPanel>
                        <cc:LabelledTextbox LabelText="Malleoli Width" Text="test" />
                        <cc:LabelledTextbox LabelText="Met Head Width" />
                    </StackPanel>
                </cc:ContentPanel>

ContentPanel.xaml

<!--ContentPanel UserControl-->
<UserControl
    x:Class="OrthoticTabletApp.Controls.ContentPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:OrthoticTabletApp.Controls"
    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"
    x:Name="Parent">

    <Grid DataContext="{Binding ElementName=Parent}">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Padding="10" Grid.Column="0" Grid.Row="0" Height="50" Background="{Binding Path=PanelHeadingBackground}">
            <TextBlock Height="30" LineHeight="30" Text="{Binding Path=PanelHeading}" />
        </Grid>

        <Grid Padding="10" Grid.Column="0" Grid.Row="1" Background="White">
            <ContentPresenter Content="{Binding Path=PanelBody}" />
        </Grid>

    </Grid>
</UserControl>

ContentPanel.xaml.cs

[ContentProperty(Name = "PanelBody")]

public sealed partial class ContentPanel : UserControl
{

    public static readonly DependencyProperty PanelHeadingProperty = DependencyProperty.Register("PanelHeading", typeof(string), typeof(ContentPanel), new PropertyMetadata(""));

    public string PanelHeading
    {
        get
        {
            return (string)GetValue(PanelHeadingProperty);
        }
        set
        {
            SetValue(PanelHeadingProperty, value);
        }
    }

    public static readonly DependencyProperty PanelBodyProperty = DependencyProperty.Register("PanelBody", typeof(object), typeof(ContentPanel), new PropertyMetadata(null));

    public object PanelBody
    {
        get
        {
            return (object)GetValue(PanelBodyProperty);
        }
        set
        {
            SetValue(PanelBodyProperty, value);
        }
    }

    public Brush PanelHeadingBackground
    {
        get { return (Brush)GetValue(PanelHeadingBackgroundProperty); }
        set { SetValue(PanelHeadingBackgroundProperty, value); }
    }

    public static readonly DependencyProperty PanelHeadingBackgroundProperty =
        DependencyProperty.Register("PanelHeadingBackground", typeof(Brush), typeof(ContentPanel), new PropertyMetadata(null));

    public ContentPanel()
    {
        this.InitializeComponent();
    }
}

LabelledTextbox.xaml

<UserControl
    x:Class="OrthoticTabletApp.Controls.LabelledTextbox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:OrthoticTabletApp.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="50"
    d:DesignWidth="400"
    x:Name="Parent">

    <Grid DataContext="{Binding ElementName=Parent}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Padding="10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="15" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="{Binding Path=LabelText}" />

        </Grid>

        <Grid Grid.Column="1" Padding="10">
            <TextBox Text="{Binding Path=Text}" />
        </Grid>
    </Grid>
</UserControl>

LabelledTextbox.xaml.cs

public sealed partial class LabelledTextbox : UserControl
    {
        public string LabelText
        {
            get { return (string)GetValue(LabelTextProperty); }
            set { SetValue(LabelTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LabelText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelTextProperty =
            DependencyProperty.Register("LabelText", typeof(string), typeof(LabelledTextbox), new PropertyMetadata(null));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(LabelledTextbox), new PropertyMetadata(null));

        public LabelledTextbox()
        {
            this.InitializeComponent();
        }
    }

但是,当我尝试绑定到ContentPanel包含的LabelledTextbox UserControl时,绑定失败,因为它只是在ContentPanel上查找ViewModel上的属性。

如果我正确理解了您所做的工作,则ContentPanel某些属性绑定到页面的视图模型中的数据模型,而LabelledTextbox属性绑定到其他视图模型中的数据模型?

如果是这样,可以为ContentPanel内部的StackPanel LabelledTextbox指定DataContext ,例如:

<cc:ContentPanel PanelHeading="{x:Bind Heading}" PanelHeadingBackground="Azure">
    <StackPanel>
        <StackPanel.DataContext>
            <local:LabelledTextboxViewModel x:Name="VM" />
        </StackPanel.DataContext>
        <cc:LabelledTextbox LabelText="{x:Bind VM.Lable1}" Text="test" />
        <cc:LabelledTextbox LabelText="{x:Bind VM.Lable2}" />
    </StackPanel>
</local:ContentPanel>

在页面的视图模型中,可以为ContentPanel创建数据模型并初始化数据,例如:

public BlankPage3ViewModel()
{
    Heading = "LEFT FOOT: Measurements";
}

public string Heading { get; set; }

在LabelledTextboxViewModel中,您可以像这样编写示例:

public class LabelledTextboxViewModel 
{
    public LabelledTextboxViewModel()
    {
        Lable1 = "Malleoli Width";
        Lable2 = "Met Head Width";
    }

    public string Lable1 { get; set; }
    public string Lable2 { get; set; }
}

通常,当我们按照MVVM模式开发项目时, 不应将数据模型包含在viewmodel内,我在这里只是为了清晰,轻松地交付 ,关键是可以为控件中的不同控件指定不同的DataContext 。同一页。

暂无
暂无

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

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