Silverlight UserControl 绑定抛出 XamlParseException、AG_E_PARSER_BAD_PROPERTY_VALUE

发布于 2024-10-23 20:57:29 字数 2339 浏览 2 评论 0原文

我已经构建了 SpinButton 用户控件。 SpinButton.xaml 具有:

<UserControl x:Class="MyApp.SpinButton" x:Name="Spinner" 
    [...]
    >

    <Grid x:Name="LayoutRoot">
        <StackPanel Margin="8,8,8,0" VerticalAlignment="Top" Orientation="Horizontal">
            <TextBox x:Name="Text" TextWrapping="Wrap" Text="{Binding Count, Mode=TwoWay, ElementName=Spinner}" TextAlignment="Center" Width="120" InputScope="TelephoneNumber"/>
            <Button x:Name="PlusButton" Content="+" BorderThickness="3,3,0,3" Margin="-12,0,0,0" Width="55" Click="PlusButton_Click" Padding="0" Style="{StaticResource ButtonStyle}" />
            <Button x:Name="MinusButton" Content="-" Width="55" Click="MinusButton_Click" Padding="0" Style="{StaticResource ButtonStyle}" />
        </StackPanel>
    </Grid>
</UserControl>

SpinButton.xaml.cs 具有

public partial class SpinButton : UserControl, INotifyPropertyChanged
{
    private int count, min, max;

    public int Count
    {
        get { return count; }
        set { count = value; Changed("Count"); }
    }

    public int Min
    {
        get { return min; }
        set { min = value; Changed("Min"); Changed("Count"); }
    }

    public int Max
    {
        get { return max; }
        set { max = value; Changed("Max"); Changed("Count"); }
    }

    public SpinButton()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Changed(string property)
    {
        if (Count < Min) Count = Min;
        if (Count > Max) Count = Max;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    private void PlusButton_Click(object sender, RoutedEventArgs e)
    {
        Count++;
    }

    private void MinusButton_Click(object sender, RoutedEventArgs e)
    {
        Count--;
    }
}

我想在页面中使用此控件。这工作完美:

<local:SpinButton Count="20" Min="0" Max="255" />

但这不是:

<local:SpinButton Count="{Binding SomeIntProperty}" Min="0" Max="255" />

我得到的只是分配 Count 属性时出现错误 AG_E_PARSER_BAD_PROPERTY_VALUE 的 XamlParseException 。

知道可能出了什么问题以及如何修复它吗?

I've built a SpinButton user control. SpinButton.xaml has:

<UserControl x:Class="MyApp.SpinButton" x:Name="Spinner" 
    [...]
    >

    <Grid x:Name="LayoutRoot">
        <StackPanel Margin="8,8,8,0" VerticalAlignment="Top" Orientation="Horizontal">
            <TextBox x:Name="Text" TextWrapping="Wrap" Text="{Binding Count, Mode=TwoWay, ElementName=Spinner}" TextAlignment="Center" Width="120" InputScope="TelephoneNumber"/>
            <Button x:Name="PlusButton" Content="+" BorderThickness="3,3,0,3" Margin="-12,0,0,0" Width="55" Click="PlusButton_Click" Padding="0" Style="{StaticResource ButtonStyle}" />
            <Button x:Name="MinusButton" Content="-" Width="55" Click="MinusButton_Click" Padding="0" Style="{StaticResource ButtonStyle}" />
        </StackPanel>
    </Grid>
</UserControl>

And SpinButton.xaml.cs has

public partial class SpinButton : UserControl, INotifyPropertyChanged
{
    private int count, min, max;

    public int Count
    {
        get { return count; }
        set { count = value; Changed("Count"); }
    }

    public int Min
    {
        get { return min; }
        set { min = value; Changed("Min"); Changed("Count"); }
    }

    public int Max
    {
        get { return max; }
        set { max = value; Changed("Max"); Changed("Count"); }
    }

    public SpinButton()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Changed(string property)
    {
        if (Count < Min) Count = Min;
        if (Count > Max) Count = Max;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    private void PlusButton_Click(object sender, RoutedEventArgs e)
    {
        Count++;
    }

    private void MinusButton_Click(object sender, RoutedEventArgs e)
    {
        Count--;
    }
}

I want to use this control in a page. This works perfectly:

<local:SpinButton Count="20" Min="0" Max="255" />

But this doesn't:

<local:SpinButton Count="{Binding SomeIntProperty}" Min="0" Max="255" />

All I'm getting is a XamlParseException with error AG_E_PARSER_BAD_PROPERTY_VALUE when assigning the Count attribute.

Any idea what could be wrong and how I could fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-10-30 20:57:29

我相信 Count 需要是 DependencyProperty 才能支持数据绑定。

I believe Count needs to be a DependencyProperty in order to support data binding.

梦罢 2024-10-30 20:57:29

将 Count 更改为依赖属性...这应该有所帮助。

自定义控件上的可绑定属性应该是依赖属性。

Change Count to a dependency property...that should help.

Bindable properties on custom controls should be dependency properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文