繁体   English   中英

用户控制可混合性WP7

[英]Usercontrol blendability wp7

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

<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();
    }
}

现在所有作品都可以使用,但不能混合使用……在苹果酒中,我看不到“测试邮件”

有一种有效的方法:)不涉及xmlns:MyControl = ...

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

但是,从您的评论看来,您似乎需要设计时数据,而不是要使控件可混合。 看一下Silverlight中有关设计时属性的MSDN部分。 特别是d:DataContext ,这在WP7中很好用。

除了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。

暂无
暂无

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

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