【发布时间】:2014-11-11 03:03:07
【问题描述】:
您好,我正在尝试绑定用户控件属性,但到目前为止还没有成功。下面是我的代码。
<UserControl x:Class="MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="120" d:DesignWidth="120"
x:Name="uc"
Background="Transparent">
<Grid>
<Viewbox>
......
<CheckBox Name="Start" Visibility="Hidden" IsChecked="{Binding Path=Animation, ElementName=uc, Mode=TwoWay}"/>
.......
</Viewbox>
</Grid>
</UserControl>
后面的用户控制代码。
public partial class MyControl: UserControl
{
public static readonly DependencyProperty AnimationProperty = DependencyProperty.Register("Animation", typeof(bool), typeof(CircularProgressBar));
public bool Animation
{
get { return (bool)GetValue(AnimationProperty); }
set
{
SetValue(AnimationProperty, value);
}
}
public MyControl()
{
InitializeComponent();
//(this.Content as FrameworkElement).DataContext = this;
}
}
查看代码
<local:MyControl x:Name="cpb" Animation="{Binding CpbIsEnabled, Mode=TwoWay}" />
查看模型代码
private bool cpbEnabled;
public bool CpbIsEnabled
{
get { return cpbEnabled; }
set { cpbEnabled = value; OnPropertyChanged("CpbIsEnabled"); }
}
public ICommand ShowSelFlagCommand
{
get
{
return showSelFlagCommand ?? (showSelFlagCommand = new DelegateCommand(() =>
{
if (CpbIsEnabled) { CpbIsEnabled = false; }
else { CpbIsEnabled = true; }
}
));
}
}
我在用户控件代码后面的动画属性设置方法中设置了一个断点。但是当视图模型中的 ICOCommand 执行时,它永远不会到达断点。换句话说,Animation 属性不是由绑定设置的。
谁能告诉我我错过了什么?
谢谢,
【问题讨论】:
-
动画属性设置方法不会被绑定调用。该属性仅在获取或设置代码时使用
-
Animation 属性不是由绑定设置的。我怎样才能让它发挥作用?
-
我相信您使用标准的 Property 语法作为 DependencyProperty 的包装器。 getter 很好,但是 setter 应该只调用 SetValue(AnimationProperty, value);
-
终极面部护理。如果是 DependencyProperty,则不需要实现 INPC。
-
我将设置器更改为调用 SetValue(AnimationProperty, value) 但仍然无法正常工作。还有什么?