2012-10-10 25 views
1

我有以下XAML將數據綁定到填充的CheckBox列表框:如何在保留選擇樣式的同時綁定CheckBox的ListBox?

 <ListBox Background="Transparent" Grid.Column="8" Grid.Row="3" ItemsSource="{Binding Path=StakeTypes}" Foreground="White" 
     Name="lbStakes" ItemContainerStyle="{StaticResource SelectionListBoxItem}" SelectionChanged="lbStakes_SelectionChanged" SelectionMode="Extended"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <ListBoxItem IsSelected="{Binding IsSelected, Mode=TwoWay}" HorizontalContentAlignment="Stretch"> 
        <CheckBox Cursor="Hand" IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Text}" HorizontalContentAlignment="Stretch" /> 
       </ListBoxItem> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

問題是,用於列表框選擇的樣式不同於如果我手動創建的每個列表框檔案:

<ListBox Background="Transparent" Cursor="Hand" Grid.Column="6" Grid.Row="3" Name="lbStakes" SelectionMode="Extended"> 
     <ListBoxItem IsSelected="True"> 
      <CheckBox IsChecked="True" Foreground="White" Content="Low" /> 
     </ListBoxItem> 
     <ListBoxItem IsSelected="True"> 
      <CheckBox IsChecked="True" Foreground="White" Content="Medium" /> 
     </ListBoxItem> 
     <ListBoxItem IsSelected="True"> 
      <CheckBox IsChecked="True" Foreground="White" Content="High" /> 
     </ListBoxItem> 
     <ListBoxItem IsSelected="True"> 
      <CheckBox IsChecked="True" Foreground="White" Content="Highest" /> 
     </ListBoxItem> 
    </ListBox> 

以下是圖像:

With Binding

Without Binding

我希望它看起來像第二個圖像。任何想法,不勝感激。

更新:下面是我想申請到ListBoxItem的樣式:

<Style x:Key="SelectionListBoxItem" TargetType="ListBoxItem"> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border Name="Border" Padding="3" SnapsToDevicePixels="true"> 
        <ContentPresenter /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter TargetName="Border" Property="Background" Value="{StaticResource myBrush}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

我不是很積極我明白這個問題......你問如何設計'SelectedItem'(如果是這樣,你可以包括XAML的'SelectionListBoxItem'?如何同步'ListBoxItem.IsSelected'和'CheckBox.IsChecked'?或者如何確保當用戶點擊'ListBoxItem'內的控件時,如'CheckBox'上的項目被選中? – Rachel

回答

1

兩種風格是不同的,因爲在被綁定的ListBox您使用ItemContainerStyle="{StaticResource SelectionListBoxItem}",而在你的第二個SNIPPIT,默認列表框項目樣式適用。嘗試從綁定列表框中刪除此樣式分配。

+0

這沒有奏效。對不起,-1,我把它設置爲+1,但希望它回到0. – NickV

+0

好吧沒有probs :) - 所以,什麼不工作?刪除ItemContainerStyle沒有任何區別,或者做了其他的事情出錯了嗎? – Surfbutler

+0

它完全沒有影響風格。 – NickV

0
<Window.Resources> 
    <Style x:Key="SelectionListBoxItem" TargetType="ListBoxItem"> 
     <Setter Property="KeyboardNavigation.TabNavigation" Value="None"></Setter> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border Name="Border" BorderBrush="Black" BorderThickness="0.5" SnapsToDevicePixels="true"> 
         <ContentPresenter /> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter TargetName="Border" Property="Background" Value="Red"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</Window.Resources> 
<Grid> 
    <ListBox Background="Transparent" Grid.Column="8" Grid.Row="3" ItemsSource="{Binding Path=IsSelectede}" Foreground="White" 
    Name="lbStakes" ItemContainerStyle="{StaticResource SelectionListBoxItem}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <ListBoxItem IsSelected="{Binding IsSelected, Mode=TwoWay}" HorizontalContentAlignment="Stretch" KeyboardNavigation.TabNavigation="None"> 
        <CheckBox Cursor="Hand" IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Text}" HorizontalContentAlignment="Stretch" /> 
       </ListBoxItem> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

一些綁定屬性的改變,請根據自己的特性。而你也是的brush.I風格糾正他們希望這將幫助。

相關問題