本文介绍了用户控制可混合性 wp7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I'd like to make a simply user control

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

现在一切正常,但不可混合......在苹果酒中我看不到消息中的测试"

now all works but is not blendable ... and in Cider I can't see "test in a message"

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

there's a way that works :) without involve xmlns:MyControl=...

推荐答案

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

Most people consder that a control is Blendable if you can edit its template. To do this, you will have to change it from a user-control to a custom-control so that its template is defined in gerenic.xaml.

但是,根据您的评论,您似乎需要设计时数据,而不是能够使控件成为可混合的.查看 MSDN 部分中的 设计时属性银光.特别是 d:DataContext,这在 WP7 中工作得很好.

However, from your comments it sounds like you need design-time data, rather than to be able to make the control Blendable. Take a look at the MSDN section on design-time attributes in Silverlight. Specifically d:DataContext, this works just fine in WP7.

这篇关于用户控制可混合性 wp7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 07:34