2013-04-10 60 views
0

gehho's answer to "MVVM Group Radio Button"我想一個單選按鈕添加到列表項的XAML添加一個單選按鈕到WPF列表項,使得當選擇列表項的單選按鈕被選中,並在列表項未選中單選按鈕未選中。凱利有a great answer他或她的「綁定單選按鈕來器isChecked ListBoxItem的IsSelected和列表框IsFocused」的問題,看起來像它應該做什麼,我需要。然而,我對凱利的XAML的簡單轉換不起作用 - 單選按鈕未被選中。這裏是我的MainWindow.xaml的使用的ControlTemplate

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="RadioButton.MainWindow" 
    x:Name="Window" 
    Title="MainWindow" 
    Width="640" Height="480"> 

    <Window.Resources> 
     <SampleData:SampleDataSource x:Key="SampleDataSource" d:IsDataSource="True"/> 
     <ControlTemplate x:Key="LBItemControlTemplate" TargetType="{x:Type ListBoxItem}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="20"/> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock TextWrapping="Wrap" Text="{Binding Property1}" Grid.Column="1"/> 
       <RadioButton x:Name="rbIsSelected" IsChecked="False" IsEnabled="False"/> 
      </Grid> 
      <ControlTemplate.Triggers> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsSelected" Value="True"/> 
         <Condition Property="Selector.IsSelectionActive" Value="True"/> 
        </MultiTrigger.Conditions> 
        <Setter Property="IsChecked" TargetName="rbIsSelected" Value="True"/> 
       </MultiTrigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     <DataTemplate x:Key="ListBoxItemTemplate"> 
      <ListBoxItem Template="{StaticResource LBItemControlTemplate}"/> 
     </DataTemplate> 
    </Window.Resources> 

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}"> 
     <ListBox ItemsSource="{Binding Collection}" ItemTemplate="{DynamicResource ListBoxItemTemplate}" SelectionMode="Single"/> 
    </Grid> 
</Window> 

任何人都可以看到這是爲什麼不工作,並提出修復或不同的方法全部?

回答

1

你知道ItemsControls會自動創建爲每個數據項的容器的項目發現了什麼?您的DataTemplate包含一個不正確的ListBoxItem。你基本上說:「當你在ListBoxItem中顯示我的內容時,創建一個新的ListBoxItem」。所以,你的代碼relys您分離ListBoxItem,它永遠不能被選中,因爲它不是在ItemsControl容器項目,因此它不能正常工作。你DataTemplate只集裝箱裏面的內容,在這種情況下,應該DataTemplate中定義數據應該是什麼樣子的ListBoxItem

這是一個小而不能necessarly好的做法的例子,但它表明你不必創建一個ListBoxItem,它就會自動出現:

某處作爲一種資源,Window.Resources或App.xaml中例如:

<DataTemplate x:Key="myTemplate"> 
    <StackPanel Orientation="Horizontal"> 
     <CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"/> 
     <TextBlock Text="{Binding}"/> 
    </StackPanel> 
</DataTemplate> 

和使用本身

<ListBox x:Name="test" ItemTemplate="{StaticResource myTemplate}"/> 

這種情況下的列表框需要在後面的代碼中獲取一個ItemsSource集。

+0

完美 - 這是'RelativeSource' /'FindAncestor' /'AncestorType'的東西,我是完全遺忘。這正是我想要的,並且根據需要使用'RadioButton'而不是'Checkbox'。感謝您的溫柔翔實的解釋。 – dumbledad 2013-04-10 12:21:21