次のように、インデックス プロパティを含める独自のラジオボタンを作成しました。
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}"
ます。
ありがとう、