RadioButtonList的兩種實現方式

一種是修改ItemTemplate,即ListBoxItem裏面的内容

                <ListBox ItemsSource="{Binding}">

                    <ListBox.ItemTemplate>

                        <DataTemplate>

                            <RadioButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"

                                         Content="{Binding XPath=@Name}" GroupName="data"/>

                        </DataTemplate>

                    </ListBox.ItemTemplate>

                </ListBox>
 

這種方式改動小,依然可以看到ListBox的選擇條

 

另一種是修改ItemContainerStyle,即直接修改ListBoxItem

                <ListBox ItemsSource="{Binding}">

                    <ListBox.ItemContainerStyle>

                        <Style TargetType="ListBoxItem">

                            <Setter Property="Template">

                                <Setter.Value>

                                    <ControlTemplate TargetType="ListBoxItem">

                                        <RadioButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"

                                                     Content="{Binding XPath=@Name}"/>

                                    </ControlTemplate>

                                </Setter.Value>

                            </Setter>

                        </Style>

                    </ListBox.ItemContainerStyle>

                </ListBox>
 

這種方式改動大,已經看不到ListBox的選擇條,感覺上不像一個ListBox控件了

 

你可能感兴趣的:(RadioButton)