簡體   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