1

私はSpinButtonユーザーコントロールを作成しました。 SpinButton.xamlがありますXamlParseExceptionをスローするSilverlight UserControlバインディング、AG_E_PARSER_BAD_PROPERTY_VALUE

<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" /> 

しかし、これはしません:count属性を割り当てるときに、私は取得しています

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

すべてのエラーAG_E_PARSER_BAD_PROPERTY_VALUEとXamlParseExceptionです。

何が間違っている可能性がありますか、どのように修正することができますか?

答えて

3

データバインディングをサポートするには、CountがDependencyPropertyである必要があります。

+0

ありがとうございます!それはそれを解決した:) –

1

変更を依存関係プロパティに変更してください。役立つはずです。

カスタムコントロールのバインド可能なプロパティは、依存関係のプロパティである必要があります。

関連する問題