(编辑修复了DP的语法错误 - 但问题仍然存在)
我在一个简单的自定义用户控件中的Databinding to Dependency Property遇到了一些问题,并想知道你们中的一些专家是否可以告诉我哪里出错了。
基本上,在我的usercontrol中,我有一个标签和两个按钮。标签绑定到我的依赖属性,当您单击按钮时,计数会根据按钮的点击次数递增或递减,然后由标签显示。
如果我在没有用户控件的页面内完成所有这些操作,那么一切正常。然而,一旦我将它移动到用户控制,它开始奇怪地行动。初始计数0在开始时按预期显示,当我单击按钮时,第一次单击时显示修订计数。但是,如果再次单击相同的按钮,将更新依赖项属性,但不会显示该值(标签变为空白)。如果我点击另一个按钮,则会显示修改后的计数,但只是第一次点击 - 这一切都非常奇怪,我被卡住了!
如果我在'TheCountProperty_Changed'方法上设置断点,我可以验证属性值是否正在正确更新。
这是我自定义控件的XAML:
<UserControl
x:Class="DPTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DPTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="TestControl"
x:Uid="TestControl">
<Grid>
<TextBlock x:Name="lblCount" HorizontalAlignment="Left" Margin="0,0,0,0" TextWrapping="Wrap" Text="{Binding Path=TheCount, ElementName=TestControl, Mode=OneWay}" VerticalAlignment="Top" FontSize="36"/>
<Button x:Name="btnLess" Content="<" HorizontalAlignment="Left" Margin="0,71,0,0" VerticalAlignment="Top" Width="75" Click="btnLess_Click" ClickMode="Press"/>
<Button x:Name="btnMore" Content=">" HorizontalAlignment="Left" Margin="91,71,0,0" VerticalAlignment="Top" Width="75" Click="btnMore_Click" ClickMode="Press"/>
</Grid>
以下是它的CS:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace DPTest
{
public sealed partial class UserControl1 : UserControl
{
public static readonly DependencyProperty TheCountProperty = DependencyProperty.Register("TheCount", typeof(int), typeof(UserControl1), new PropertyMetadata(0, new PropertyChangedCallback(TheCountProperty_Changed)));
public int TheCount
{
get { return (int)GetValue(TheCountProperty); }
set { SetValue(TheCountProperty, value); }
}
private static void TheCountProperty_Changed(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
}
public UserControl1()
{
this.InitializeComponent();
}
private void btnLess_Click(object sender, RoutedEventArgs e)
{
//lblCount.Text = (Convert.ToInt32(lblCount.Text) - 1).ToString();
this.TheCount -= 1;
}
private void btnMore_Click(object sender, RoutedEventArgs e)
{
//lblCount.Text = (Convert.ToInt32(lblCount.Text) + 1).ToString();
this.TheCount += 1;
}
}
}
我假设我的Databinding语法略有不正确,但应该是什么?
(编辑) - 情节变浓!想要补充一点,我刚刚在标准的WPF应用程序中尝试了完全相同的语法,一切都按预期工作。所以我想问题是,我是否发现了WinRT / XAML的错误,或者使用该平台的语法不同?
(编辑#2抱歉所有的编辑,但我仍然试图解决这个问题!)我开始相信这是一个显示问题或按钮控件的错误。我刚刚添加了一个额外的按钮,但没有为它或任何东西连接任何点击事件代码 - 它基本上只是一个什么都不做的按钮。这是有趣的事情:
这非常奇怪......
答案 0 :(得分:2)
DependencyProperty的第三个参数看起来应该是'UserControl1'而不是'UserControl'。
在启动Win 8并为自己尝试代码后,我看到了你遇到的同样问题。
由此我确定了两件事:
1)你的依赖属性基本上是正确的。我可以在点击按钮时观察属性增加和减少的值。这告诉我,问题是某种刷新问题。
2)将代码从UserControl移动到Main xaml页面(在我的测试应用程序中,它是默认的'Blank.xaml'页面)允许数字正确刷新。
在我看来,测试版(错误的“消费者预览版”)存在某种刷新错误,您可以通过稍微不同地构建应用程序来解决这个问题。
答案 1 :(得分:0)
我通过继承RichTextBox做了类似的事情。
我看到我没有自己的依赖属性(不确定是否重要)
另外,我假设你打算填充这个:
private static void TheCountProperty_Changed(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
}