2010-06-12 48 views

回答

1

這是一個Style,它使用ListBox創建一組邏輯相關的RadioButton。 MyClass包含兩個字符串屬性:MyName和MyToolTip。該樣式將顯示RadioButtons的列表,包括正確運行的各個工具提示。這是一個全部綁定的,所有用於MVVM的Xaml解決方案。

實例:

列表框形式= 「{StaticResource的radioListBox}」 的ItemsSource = 「{結合MyClasses}」 的SelectedValue = 「{結合SelectedMyClass}」/>

風格:

<Style x:Key="radioListBox" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}"> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="Background" Value="{x:Null}" /> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListBoxItem"> 
          <Grid Background="Transparent"> 
           <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}" Content="{Binding MyName}"/> 
          </Grid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="ToolTip" Value="{Binding MyToolTip}" /> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style>