【发布时间】:2014-02-01 20:03:35
【问题描述】:
我有依赖属性的用户控制。当我尝试在 Windows 中绑定它时,它不起作用。但是当我将绑定更改为普通参数时(在这种情况下,是的)它正在工作。我需要做什么才能使我的绑定工作?也许与DataContext有关?这是我的代码:
第一个用户控件的 xaml:
<UserControl x:Class="DPTest.CustomUserControl"
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="100" d:DesignWidth="200"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<TextBlock Text="{Binding Property}"/>
</Grid>
</UserControl>
后面的代码:
public partial class CustomUserControl : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty DPTestProperty = DependencyProperty.Register("DPTest", typeof(bool), typeof(CustomUserControl), new PropertyMetadata(default(bool), propertyChangedCallback));
public bool DPTest
{
get
{
return (bool)GetValue(DPTestProperty);
}
set
{
SetValue(DPTestProperty, value);
}
}
private bool _property;
public bool Property
{
get
{
return _property;
}
set
{
_property = value;
RaisePropertyChanged("Property");
}
}
private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as CustomUserControl;
control.Property = (bool)e.NewValue;
}
public CustomUserControl()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在我放置此用户控件数据上下文的窗口中,将其设置为我的视图模型:
<phone:PhoneApplicationPage
x:Class="DPTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:my="clr-namespace:DPTest"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<my:CustomUserControl Name="cstom" DPTest="{Binding Property}"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
在视图模型中,我有绑定到 DPTest 的属性。
public class MainViewModel : ViewModelBase
{
private bool _property;
public bool Property
{
get { return _property; }
set
{
_property = value;
RaisePropertyChanged("Property");
}
}
public MainViewModel()
{
Property = true;
}
}
当我运行这个应用程序时,Textblock 的值为 False,但这应该是 True,并且 propertyChangedCallback 甚至没有被调用。我需要做什么?
谢谢
【问题讨论】:
-
{Binding Main, Source={StaticResource Locator}}中的Main和Locator是什么? -
它是使用 MvvmLight 添加的 ViewModelLocator 的实例。它适用于普通的 TextBlock(当我运行它时它的值为 True)。
-
所以
Locator.Main属于MainViewModel类型并且具有Property属性。如果在同一页面上添加<Checkbox IsChecked={Binding Property}/>,它会被检查吗? -
是的,和你写的完全一样。
-
而且,正如您所提到的,如果您使用
DPTest="True"而不是绑定,则TextBlock显示为 true。对吗?
标签: c# wpf windows-phone-8