3

(已编辑以修复 DP 的语法错误 - 但问题仍然存在)

我在简单的自定义用户控件中将数据绑定到依赖属性时遇到了一些问题,想知道你们中的一些专家是否可以告诉我哪里出错了。

基本上,在我的用户控件中,我有一个标签和两个按钮。标签绑定到我的依赖属性,当您单击按钮时,计数会根据按钮单击而增加或减少,然后由标签显示。

如果我在没有用户控件的页面内执行所有这些操作,则一切正常。但是,一旦我将它移动到用户控件,它就会开始出现奇怪的行为。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="&lt;" HorizontalAlignment="Left" Margin="0,71,0,0" VerticalAlignment="Top" Width="75" Click="btnLess_Click" ClickMode="Press"/>
    <Button x:Name="btnMore" Content="&gt;" 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;
    }
}
}

我假设我的数据绑定语法有点不正确,但它应该是什么?

(编辑) - 情节变厚了!想要补充一点,我刚刚在标准 WPF 应用程序中尝试了完全相同的语法,一切都按预期工作。所以我想问题是,我是否发现了 WinRT/XAML 的错误,或者使用该平台的语法是否不同?

(编辑#2 对所有的编辑感到抱歉,但我仍在努力解决这个问题!)我开始相信这是按钮控件的显示问题或错误。我刚刚添加了一个额外的按钮,但没有为它或任何东西连接任何点击事件代码 - 它基本上只是一个什么都不做的按钮。这是有趣的事情:

  • 如果我启动测试应用程序,请单击 btnMore 按钮,然后 TheCount 会增加,并且 Textblock 会按预期更新。如果我再次单击同一个按钮,TheCount 会增加,但 lblCount 的 Text 值会变为空字符串。
  • 但是,如果我启动测试应用程序并按下 btnMore,然后按下未使用的按钮,并不断重复,那么值会不断增加,TextBlock 将不断更新——只要我不连续按两次 btnMore,但是按下空白按钮(或者实际上只是将光标聚焦到空白文本框或任何其他 UI 事物上),然后 TextBlock 将继续显示递增的数字。第二次我多次单击 btnMore,然后 lblCount 的值变成一个空字符串。

这很诡异......

4

3 回答 3

2

DependencyProperty 的第三个参数看起来应该是“UserControl1”而不是“UserControl”。

在启动 Win 8 并为自己尝试代码后,我看到了同样的问题。

由此我能够确定两件事:

1)您的依赖属性基本上是正确的。当单击按钮时,我可以观察到属性值的增加和减少。这向我表明问题是某种刷新问题。

2) 将代码从 UserControl 移动到 Main xaml 页面(在我的测试应用程序中,它是默认的“Blank.xaml”页面)允许数字正确刷新。

在我看来,测试版存在某种刷新错误(err..'consumer preview'),您可以通过稍微不同地构建应用程序来解决此问题。

于 2012-03-18T19:57:52.360 回答
1

这看起来像是 WinRT 中的一些同步问题——我使用转换器来检查我得到了什么,看起来 int 值被设置为 null。一旦我修改了代码以异步调用属性的更新 - 它开始正常工作。

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>
        <Grid.Resources>
            <local:IntToStringConverter
                x:Key="IntToStringConverter" />
        </Grid.Resources>
        <TextBlock
            x:Name="lblCount"
            HorizontalAlignment="Left"
            Margin="0,0,0,0"
            TextWrapping="Wrap"
            Text="{Binding Path=TheCount, Converter={StaticResource IntToStringConverter}, ElementName=TestControl, Mode=OneWay}"
            VerticalAlignment="Top"
            FontSize="36" />
        <Button
            x:Name="btnLess"
            Content="&lt;"
            HorizontalAlignment="Left"
            Margin="0,71,0,0"
            VerticalAlignment="Top"
            Width="75"
            Click="btnLess_Click"
            ClickMode="Release" />
        <Button
            x:Name="btnMore"
            Content="&gt;"
            HorizontalAlignment="Left"
            Margin="91,71,0,0"
            VerticalAlignment="Top"
            Width="75"
            Click="btnMore_Click"
            ClickMode="Release" />
    </Grid>
</UserControl>

后面的代码:

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.Dispatcher.InvokeAsync(
                Windows.UI.Core.CoreDispatcherPriority.Low,
                (s, a) => this.TheCount -= 1,
                this,
                null);
        }

        private void btnMore_Click(object sender, RoutedEventArgs e)
        {
            //lblCount.Text = (Convert.ToInt32(lblCount.Text) + 1).ToString();
            this.Dispatcher.InvokeAsync(
                Windows.UI.Core.CoreDispatcherPriority.Low,
                (s, a) => this.TheCount += 1,
                this,
                null);
        }
    }

    public class IntToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

}
于 2012-03-19T05:02:39.877 回答
0

通过从 RichTextBox 继承,我做了一些非常相似的事情。

http://www.wpfsharp.com/2011/08/01/loading-a-richtextbox-from-an-rtf-file-using-binding-or-a-richtextfile-control/

我看到我没有将我的依赖属性设为只读(不确定这是否重要)

另外,我假设你打算填充这个:

private static void TheCountProperty_Changed(DependencyObject source, DependencyPropertyChangedEventArgs e)
{

}
于 2012-03-18T20:03:43.727 回答