【问题标题】:Usercontrol blendability wp7用户控件可混合性 wp7
【发布时间】:2011-09-28 16:51:39
【问题描述】:

您好,我想做一个简单的用户控件

<UserControl x:Class="TestDependencyProps.controls.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" >
        <TextBlock Height="30" Margin="31,140,27,0" Name="textBlock1" Text="{Binding testMessage}" VerticalAlignment="Top" />
    </Grid>

</UserControl>

后面的代码:

public partial class TestControl : UserControl
{
    public string testMessage
    {
        get { return (string)GetValue(testMessageProperty); }
        set { SetValue(testMessageProperty, value); }
    }


    public static readonly DependencyProperty testMessageProperty =
        DependencyProperty.Register("testMessage", typeof(string), typeof(TestControl),new PropertyMetadata("test in a message",null)
        );

    public TestControl()
    {
        InitializeComponent();
    }
}

现在一切正常,但不可混合......在 Cider 中我看不到“消息中的测试”

有一种方法可以工作 :) 不涉及 xmlns:MyControl=...

【问题讨论】:

  • 您必须更清楚您对“可混合”的定义。 精确的是行不通的。
  • 我的意思是我在设计时看到,在运行应用程序之前,屏幕上出现“test in a message”消息:)
  • 你完全误解了依赖属性是如何工作的。

标签: xaml windows-phone-7 user-controls


【解决方案1】:

如果您可以编辑其模板,大多数人认为控件是可混合的。为此,您必须将其从用户控件更改为自定义控件,以便在 gerenic.xaml 中定义其模板。

但是,从您的 cmets 看来,您需要设计时数据,而不是能够使控件可混合。查看design-time attributes in Silverlight 上的 MSDN 部分。特别是d:DataContext,这在 WP7 中运行良好。

【讨论】:

    【解决方案2】:

    除了 ColinE 的回答之外,您可能需要更改一些代码以使您的依赖项属性与设计时数据一起使用,

        public string testMessage
        {
            get { return (string)GetValue(testMessageProperty); }
            set { SetValue(testMessageProperty, value); }
        }
    
    public static readonly DependencyProperty testMessageProperty =
                DependencyProperty.Register("testMessage", typeof(string), typeof(TestControl), new PropertyMetadata("test in a message", PropertyChangedCallback));
    
            private static void PropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                if (e.NewValue != null)
                {
                    var c = (TestControl)sender;
                    // assign value to the TextBlock here
                    c.textBlock1.Text = e.NewValue.ToString();
                }
            }
    

    并删除 TextBlock Text="{Binding testMessage}" 中的绑定。

    要在设计时显示文本,您需要添加一个设计时 DataContext(就像 ColinE 建议的那样),

    <Grid x:Name="LayoutRoot" d:DataContext="{d:DesignInstance design:DesignMainViewModel, IsDesignTimeCreatable=True}">
        <xxx:TestControl testMessage={Binding SomeText} />
    </Grid>
    

    希望这会有所帮助。 :)

    编辑

    实际上,正如 Colin 指出的那样,如果您命名用户控件并使用 ElementName 绑定而不是普通绑定,则不需要回调。

    <UserControl x:Name="myUserControl" ...
    

    在 TextBlock 中,您可以这样做

    Text="{Binding testMessage, ElementName=myUserControl}"
    

    简单地绑定到 testMessage 是行不通的,因为这个属性是 UserControl 的。

    【讨论】:

    • 只是出于兴趣,您为什么认为需要 DP PropertyChnagedCallback? XAML Text="{Binding testMessage}" 中的绑定将确保 textBlock1 获取设计时数据。
    • 如果我错了请纠正我,但我认为 TextBlock 不能访问 UserControl 的依赖属性?
    • 其实还是可以通过绑定完成的,请看我更新的答案。 :)
    • oops @ColinE 我刚刚意识到我必须@你才能让你看到我的回复。 :)
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2011-09-21
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2011-05-04
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多