【问题标题】:Silverlight UserControl binding throwing a XamlParseException, AG_E_PARSER_BAD_PROPERTY_VALUESilverlight UserControl 绑定引发 XamlParseException、AG_E_PARSER_BAD_PROPERTY_VALUE
【发布时间】:2011-07-16 19:50:47
【问题描述】:

我已经构建了一个 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" />

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

知道可能出了什么问题以及如何解决吗?

【问题讨论】:

    标签: silverlight data-binding windows-phone-7 user-controls windows-phone


    【解决方案1】:

    我认为 Count 需要成为 DependencyProperty 才能支持数据绑定。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-22
        • 2010-10-18
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2011-08-31
        相关资源
        最近更新 更多