Xaml 两个用户控件,一个绑定,一个不';T

Xaml 两个用户控件,一个绑定,一个不';T,xaml,windows-runtime,winrt-xaml,Xaml,Windows Runtime,Winrt Xaml,在一个简单的WinRT演示项目中,我有两个非常简单的用户控件。没有Viewmodel——大部分只是用于布局探索的彩色框。我创建的第一个UserControl运行良好。第二个非常类似,不会绑定到任何属性——它显示为空白 第一个UserControl如下所示: <UserControl x:Class="Demo.SmallStartTile" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio

在一个简单的WinRT演示项目中,我有两个非常简单的用户控件。没有Viewmodel——大部分只是用于布局探索的彩色框。我创建的第一个UserControl运行良好。第二个非常类似,不会绑定到任何属性——它显示为空白

第一个UserControl如下所示:

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


    <Grid x:Name="SmallTileGrid"
        Margin="0,0,8,8"
        Background="{Binding Background, ElementName=SmallStartTileUC}"
        >

        <Rectangle
            Stroke="{Binding BorderBrush, ElementName=SmallStartTileUC}"
            Grid.RowSpan="2"
            />

        <TextBlock x:Name="SmallTileTitle" 
            Text="{Binding TileText, ElementName=SmallStartTileUC}" 
            Style="{StaticResource SmallTileHeader}"/>

        <Path x:Name="IconPath" 
            Style="{StaticResource SmallTileIcon}" 
            Data="{Binding TileIconPathData, ElementName=SmallStartTileUC}" />

    </Grid>




    </UserControl>

namespace Demo
{
    public sealed partial class SmallStartTile : UserControl
    {
    public static DependencyProperty TileTextProperty = DependencyProperty.Register("TileText", typeof(string), typeof(SmallStartTile), new PropertyMetadata("tile content"));

    public string TileText
    {
        get { return (string)GetValue(TileTextProperty); }
        set { SetValue(TileTextProperty, value); }
    }


    public static DependencyProperty TileIconPathDataProperty = DependencyProperty.Register("TileIconPathData", typeof(string), typeof(SmallStartTile), new PropertyMetadata("F0"));

    public string TileIconPathData
    {
        get { return (string)GetValue(TileIconPathDataProperty); }
        set { SetValue(TileIconPathDataProperty, value); }
    }



    public SmallStartTile()
    {
        this.InitializeComponent();
    }
}
                            <local:SmallMediaTile x:Name="Source1Tile"
                            Grid.Column="0" Grid.Row="0"
                            SourceText="Radio"
                            Background="Blue"
                            BorderBrush="Red">
                            <local:SmallMediaTile.Content>
                            <Canvas x:Name="radio_icon" Height="68" Width="34">
                                <Path Data="F1M299.6182,396.2988C299.6182,389.7208,297.0722,383.5548,292.4572,378.8398L288.6162,382.6758C292.2022,386.3718,294.1842,391.1778,294.1842,396.2988C294.1842,401.4238,292.2022,406.2368,288.6162,409.9328L292.4572,413.7738C297.0722,409.0538,299.6182,402.8808,299.6182,396.2988" Fill="White" Height="34.934" Canvas.Left="0" Stretch="Fill" Canvas.Top="16.501" Width="11.002"/>
                                <Path Data="F1M311.1738,396.2988C311.1738,386.6278,307.4348,377.5528,300.6788,370.6208L296.8258,374.4618C302.5658,380.3698,305.7398,388.0798,305.7398,396.2988C305.7398,404.5218,302.5658,412.2298,296.8258,418.1428L300.6788,421.9898C307.4348,415.0498,311.1738,405.9718,311.1738,396.2988" Fill="White" Height="51.369" Canvas.Left="8.21" Stretch="Fill" Canvas.Top="8.282" Width="14.348"/>
                                <Path Data="F1M322.7578,396.2988C322.7578,383.5298,317.8508,371.5348,308.9638,362.3388L305.1168,366.1748C312.9758,374.3538,317.3338,384.9778,317.3338,396.2988C317.3338,407.6208,312.9758,418.2488,305.1168,426.4268L308.9638,430.2748C317.8508,421.0698,322.7578,409.0798,322.7578,396.2988" Fill="White" Height="67.936" Canvas.Left="16.501" Stretch="Fill" Canvas.Top="0" Width="17.641"/>
                            </Canvas>
                            </local:SmallMediaTile.Content>
                        </local:SmallMediaTile>

名称空间演示
{
公共密封部分类SmallStartTile:UserControl
{
public static dependencProperty TileTextProperty=dependencProperty.Register(“TileText”、typeof(string)、typeof(SmallStartTile)、new PropertyMetadata(“tile内容”);
公共字符串TiletText
{
获取{return(string)GetValue(TileTextProperty);}
set{SetValue(TileTextProperty,value);}
}
public static dependencProperty TileIconPathDataProperty=dependencProperty.Register(“TileIconPathData”、typeof(string)、typeof(SmallStartTile)、new PropertyMetadata(“F0”);
公共字符串TileConPathData
{
获取{return(string)GetValue(TileIconPathDataProperty);}
set{SetValue(TileIconPathDataProperty,value);}
}
公共SmallStartTile()
{
this.InitializeComponent();
}
}
}

第二个,我通过单击Add New Item并在Blend中选择UserControl,制作了与第一个一样的:

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

<Grid 
    Margin="0,0,8,8"
    Background="{Binding Background, ElementName=SmallMediaTileUC}"
    >

    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Rectangle 
        Stroke="{Binding BorderBrush, ElementName=SmallMediaTileUC}"
        Grid.RowSpan="2"
        />

    <Viewbox 
        Margin="30" 
        Child="{Binding Content, ElementName=SmallMediaTileUC}">
    </Viewbox>

    <TextBlock 
        Grid.Row="1" 
        Text="{Binding SourceText, ElementName=SmallMediaTileUC}" 
        Style="{StaticResource SmallMusicTileHeader}"/>

</Grid>

</UserControl>


namespace Demo
{
    public sealed partial class SmallMediaTile : UserControl
    {

    public static DependencyProperty SourceTextProperty = DependencyProperty.Register("SourceText", typeof(string), typeof(SmallMediaTile), new PropertyMetadata("source"));

    public string SourceText
    {
        get { return (string)GetValue(SourceTextProperty); }
        set { SetValue(SourceTextProperty, value); }
    }



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

名称空间演示
{
公共密封部分类SmallMediaTile:UserControl
{
public static dependencProperty SourceTextProperty=dependencProperty.Register(“SourceText”、typeof(string)、typeof(SmallMediaTile)、newpropertyMetadata(“source”);
公共字符串源文本
{
获取{return(string)GetValue(SourceTextProperty);}
set{SetValue(SourceTextProperty,value);}
}
公共小型媒体()
{
this.InitializeComponent();
}
}
}

我在主页面中使用UserControl,如下所示:

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


    <Grid x:Name="SmallTileGrid"
        Margin="0,0,8,8"
        Background="{Binding Background, ElementName=SmallStartTileUC}"
        >

        <Rectangle
            Stroke="{Binding BorderBrush, ElementName=SmallStartTileUC}"
            Grid.RowSpan="2"
            />

        <TextBlock x:Name="SmallTileTitle" 
            Text="{Binding TileText, ElementName=SmallStartTileUC}" 
            Style="{StaticResource SmallTileHeader}"/>

        <Path x:Name="IconPath" 
            Style="{StaticResource SmallTileIcon}" 
            Data="{Binding TileIconPathData, ElementName=SmallStartTileUC}" />

    </Grid>




    </UserControl>

namespace Demo
{
    public sealed partial class SmallStartTile : UserControl
    {
    public static DependencyProperty TileTextProperty = DependencyProperty.Register("TileText", typeof(string), typeof(SmallStartTile), new PropertyMetadata("tile content"));

    public string TileText
    {
        get { return (string)GetValue(TileTextProperty); }
        set { SetValue(TileTextProperty, value); }
    }


    public static DependencyProperty TileIconPathDataProperty = DependencyProperty.Register("TileIconPathData", typeof(string), typeof(SmallStartTile), new PropertyMetadata("F0"));

    public string TileIconPathData
    {
        get { return (string)GetValue(TileIconPathDataProperty); }
        set { SetValue(TileIconPathDataProperty, value); }
    }



    public SmallStartTile()
    {
        this.InitializeComponent();
    }
}
                            <local:SmallMediaTile x:Name="Source1Tile"
                            Grid.Column="0" Grid.Row="0"
                            SourceText="Radio"
                            Background="Blue"
                            BorderBrush="Red">
                            <local:SmallMediaTile.Content>
                            <Canvas x:Name="radio_icon" Height="68" Width="34">
                                <Path Data="F1M299.6182,396.2988C299.6182,389.7208,297.0722,383.5548,292.4572,378.8398L288.6162,382.6758C292.2022,386.3718,294.1842,391.1778,294.1842,396.2988C294.1842,401.4238,292.2022,406.2368,288.6162,409.9328L292.4572,413.7738C297.0722,409.0538,299.6182,402.8808,299.6182,396.2988" Fill="White" Height="34.934" Canvas.Left="0" Stretch="Fill" Canvas.Top="16.501" Width="11.002"/>
                                <Path Data="F1M311.1738,396.2988C311.1738,386.6278,307.4348,377.5528,300.6788,370.6208L296.8258,374.4618C302.5658,380.3698,305.7398,388.0798,305.7398,396.2988C305.7398,404.5218,302.5658,412.2298,296.8258,418.1428L300.6788,421.9898C307.4348,415.0498,311.1738,405.9718,311.1738,396.2988" Fill="White" Height="51.369" Canvas.Left="8.21" Stretch="Fill" Canvas.Top="8.282" Width="14.348"/>
                                <Path Data="F1M322.7578,396.2988C322.7578,383.5298,317.8508,371.5348,308.9638,362.3388L305.1168,366.1748C312.9758,374.3538,317.3338,384.9778,317.3338,396.2988C317.3338,407.6208,312.9758,418.2488,305.1168,426.4268L308.9638,430.2748C317.8508,421.0698,322.7578,409.0798,322.7578,396.2988" Fill="White" Height="67.936" Canvas.Left="16.501" Stretch="Fill" Canvas.Top="0" Width="17.641"/>
                            </Canvas>
                            </local:SmallMediaTile.Content>
                        </local:SmallMediaTile>


第一个窗口拾取所有属性并按预期显示,但第二个窗口仅拾取Viewbox内容。我看了这么多,一直在谷歌上搜索,但不知道问题出在哪里。如果这是冗长的,很抱歉。

您的根
网格
被设置为
用户控件
内容
,而您的
视图框
正试图将其
子属性
绑定到
用户控件
内容
,这是它的优势。这是不允许的,因为根
网格
已经是
用户控件
的父级,并且XAML控件只能存在于UI中的一个位置。

如前所述,问题是用户控件的内容只能设置一次。将Viewbox内容绑定到UserControl内容时,会替换现有的XAML。所以,一个空白的盒子,除了我要传递的东西。我想要做我所希望的,我需要做一个自定义控件。谢谢你的帮助

我看不到任何明显的东西,但您能在Visual Studio的调试输出窗口中检查绑定错误吗?有什么特别不起作用?当我试图将Viewbox的子控件绑定到UserControl的一个不存在的内容属性时,我可以看到控件崩溃,但是如果我删除它,它似乎工作得很好。我想我的队友找到了答案。问题是Viewbox。在页面XAML中设置Viewbox内容将覆盖UserControl的内容。所以真正的问题是:如何将画布内容插入到用户控件中?我会关闭这个问题并问那个问题。编辑这个问题以在主页中显示UserControl,这似乎就是问题所在。另一个问题的答案是-这取决于。这取决于你想如何处理这个问题。您可以使用模板化的ContentControl而不是UserControl,并修改其模板以添加TextBlock等。您还可以继续使用UserControl并添加一些其他将绑定到Viewbox内容的依赖项属性(
子项
)。