想在list控件下增加自定义的radiobutton内容,这里涉及到radiobutton与listitem冲突的地方,就是选中谁的问题,这里用到的控件内部绑定 RelativeSource, 否则会带来很多麻烦。
<ListBox Name="lbx" BorderThickness="0" Margin="5 0" ItemsSource="{Binding Infos}" SelectionChanged="SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Margin="0 3" IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem ,Mode=FindAncestor} }" >
<TextBlock>
<TextBlock VerticalAlignment="Center" Content="{Binding Name}" ></TextBlock>
</TextBlock>
</RadioButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果要设置默认值,可以通过SelectedItem设定, 比如
var v = viewmodel.FirstOrDefault(p => p.ID == 100);
list.SelectedItem = v;