0

次のように、インデックス プロパティを含める独自のラジオボタンを作成しました。

public class IndexedRadioButton : RadioButton
{
    public int Index { get; set; }  
}

リスト ボックスでこのカスタム ラジオ ボタンを使用しています。

<ListBox Name="myListBox" Grid.Row="1" VerticalAlignment="Top">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" >
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <my:IndexedRadioButton Content="{Binding price}" GroupName="myGroup" IsChecked="{Binding isChecked}" Index="{Binding priceIndex}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

ここで、このリスト ボックスに値を入力します。

コードビハインド:

public MainClass()
    {
        public MainClass()
        {
           InitializeComponent();

           string[] priceList = new string["1","2","3"];
           List<MyClass> myClassList = new List<MyClass>();

           for (int i = 0; i < priceList.Count; i++)
           {
               MyClass listClass = new MyClass()
               {
                   price =  response.priceList[i],
                   priceIndex = i,
                   isChecked = i==0?true:false
               };
               myClassList.Add(listClass);
            }

            myListBox.ItemsSource = myClassList;
        }
        private class MyClass
        {
           public string price {get; set;}
           public int priceIndex {get; set;}
           public bool isChecked { get; set; }
        }

    }

アプリを実行すると、このエラーが発生します ->>{System.ArgumentException: 値が期待される範囲内にありません。} (スタック トレース情報なし)

エラーの原因は何だと思いますか? XAML で静的に Index に値を設定しても問題はありませんがIndex="0"、バインディングに問題がありIndex="{Binding priceIndex}"ます。

ありがとう、

4

1 に答える 1

1

バインディングを許可するには、依存関係プロパティを宣言する必要があります。これを試して:

public class IndexedRadioButton : RadioButton
{
    public static readonly DependencyProperty IndexProperty = DependencyProperty.Register(
        "Index",
        typeof(int),
        typeof(IndexedRadioButton),
        null);

    public int Index
    {
        get { return (int)GetValue(IndexProperty); }
        set { SetValue(IndexProperty, value); }
    }

}

詳細については、次を参照してください。

Windows Phone の依存関係プロパティ

于 2013-06-06T14:49:30.867 に答える