[英]Usercontrol blendability 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>
Code behind: 后面的代码:
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" 现在所有作品都可以使用,但不能混合使用……在苹果酒中,我看不到“测试邮件”
there's a way that works :) without involve xmlns:MyControl=... 有一种有效的方法:)不涉及xmlns:MyControl = ...
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.
为此,您必须将其从用户控件更改为自定义控件,以便在gerenic.xaml中定义其模板。
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 .
看一下Silverlight中有关设计时属性的MSDN部分。 Specifically
d:DataContext
, this works just fine in WP7. 特别是
d:DataContext
,这在WP7中很好用。
in addition to ColinE's answer, you might need to change a bit of your code to get your dependency property working with design time data, 除了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();
}
}
and remove the binding in your TextBlock Text="{Binding testMessage}"
. 并删除TextBlock
Text="{Binding testMessage}"
。
To display the text in design time, you need to add a design time DataContext (like what ColinE suggested), 要在设计时显示文本,您需要添加设计时DataContext(如ColinE建议的那样),
<Grid x:Name="LayoutRoot" d:DataContext="{d:DesignInstance design:DesignMainViewModel, IsDesignTimeCreatable=True}">
<xxx:TestControl testMessage={Binding SomeText} />
</Grid>
Hope this helps. 希望这可以帮助。 :)
:)
EDIT 编辑
Actually as Colin pointed out, you don't need the callback, if you name your usercontrol and use ElementName binding instead of the normal binding. 实际上,正如Colin所指出的,如果您命名用户控件并使用ElementName绑定而不是常规绑定,则不需要回调。
<UserControl x:Name="myUserControl" ...
Inside the TextBlock, you do 在TextBlock中,您可以
Text="{Binding testMessage, ElementName=myUserControl}"
Simply bind to testMessage wouldn't work because this property is of the UserControl. 仅绑定到testMessage无效,因为此属性属于UserControl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.