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(TestContro开发者_如何转开发l),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=...
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.
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.
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,
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}"
.
To display the text in design time, you need to add a design time DataContext (like what ColinE suggested),
<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.
<UserControl x:Name="myUserControl" ...
Inside the TextBlock, you do
Text="{Binding testMessage, ElementName=myUserControl}"
Simply bind to testMessage wouldn't work because this property is of the UserControl.
精彩评论