Mau*_*uro 3 mvvm windows-phone
我有一个我为Windows Phone应用程序定义的简单自定义控件.控件的XAML如下:
<UserControl x:Class="PhoneApp1.MyControl"
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}"
d:DesignHeight="480"
d:DesignWidth="480"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding MyLabel}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
此外,用户控件具有依赖项属性,如下所示:
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public static readonly DependencyProperty MyLabelProperty =
DependencyProperty.Register("MyLabel", typeof (string), typeof (MyControl), new PropertyMetadata(default(string)));
public string MyLabel
{
get { return (string) GetValue(MyLabelProperty); }
set { SetValue(MyLabelProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
现在问题是当我尝试将它数据绑定到我创建的View Model时,没有任何反应.各种代码项如下:
public class MyControlViewModel : ViewModelBase
{
private string name;
public string Name
{
get { return name; }
set { name = value;
RaisePropertyChanged(() => Name);}
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型在App.xaml中声明
<Application.Resources>
<PhoneApp1:MyControlViewModel x:Key="MCVM" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
MainPage.xaml控件的声明
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<PhoneApp1:MyControl x:Name="testControl" DataContext="{StaticResource MCVM}" MyLabel="{Binding Path=MyLabel}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其数据绑定到其他UI元素,如下所示,它似乎工作?
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<PhoneApp1:MyControl x:Name="testControl" MyLabel="{Binding ElementName=Button, Path=Content}" />
<Button Click="Button_Click" x:Name="Button" Content="Click" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
我一直在努力让这个简单的场景发挥作用,我认为我有些愚蠢的错过了吗?
任何帮助将不胜感激
小智 5
(这是WPF;我不确定这是不是与WP7 silverlight 100%类似;我提供了更多细节,以帮助您了解正在发生的事情,以便您可以研究一个适当的解决方案,如果我的两个选项不可用)
默认情况下,绑定以xaml文件的根元素的DataContext为根(无论是Window还是UserControl).这意味着
<TextBlock Text="{Binding MyLabel}" />
Run Code Online (Sandbox Code Playgroud)
尝试绑定到DataContext.所以,基本上,你试图绑定的属性是
MyControl.DataContext.MyLabel
您需要绑定到以下内容:
MyControl.MyLabel
因此,您必须将绑定重新绑定到要绑定的可视树中的元素,即MyControl类.你可以通过几种方式做到这一点......
<TextBlock Text="{Binding MyLabel,
RelativeSource={RelativeSource FindAncestor,
AncestorType=UserControl}" />
Run Code Online (Sandbox Code Playgroud)
是常见的,但必须走树,可能会导致一些性能问题.我发现执行以下操作更容易,并建议:
<UserControl x:Class="PhoneApp1.MyControl"
x:Name="root"
x:SnipAttributesBecauseThisIsAnExample="true">
<TextBlock Text="{Binding MyLabel, ElementName=root}" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
为UserControl指定一个名称,然后使用ElementName将绑定重新绑定到可视树的根目录.
我会忽略其他更疯狂的方法来实现这一目标.
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |