【问题标题】:Two UserControls, one binds, one doesn't两个用户控件,一个绑定,一个不绑定
【发布时间】:2013-02-15 08:08:11
【问题描述】:

我在一个简单的 WinRT 演示项目中有两个非常简单的 UserControl。没有 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();
    }
}

}

第二个,就像第一个一样,通过单击添加新项目并在 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();
    }
}

}

我在主页中这样使用 UserControl:

                            <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 内容。我已经查看了 SO 并一直在谷歌搜索,但无法弄清楚问题所在。对不起,如果这是冗长的。

【问题讨论】:

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

标签: xaml windows-runtime winrt-xaml


【解决方案1】:

您的根Grid 设置为UserControlContent,而您拥有的Viewbox 正试图将其Child 属性绑定到UserControlContent 上.这是不允许的,因为根 Grid 已经是 UserControl 的父级,并且 XAML 控件只能存在于 UI 中的一个位置。

【讨论】:

    【解决方案2】:

    如前所述,问题在于 UserControl 的内容只能设置一次。当我将 Viewbox 内容绑定到 UserControl 内容时,现有的 XAML 将被替换。所以,一个空白框,但对于我传入的内容。我想要做我希望的事情,我需要制作一个自定义控件。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2013-08-21
      • 2012-10-13
      • 2018-12-09
      相关资源
      最近更新 更多