2010-11-23 57 views
2

我有一個數據綁定的組合框。在這個列表中,我需要一個分隔符。由於這是databound,我做了一些非常類似於this post。我的數據庫返回列表,包含一個' - '標記分隔符需要去的地方,datatrigger使它成爲分隔符。禁用WPF中的數據綁定組合框中的分隔符選擇

<ComboBox Name="cbAction" Grid.Column="1" Grid.Row="0" Margin="5,2,5,2" DisplayMemberPath="Description" SelectedValuePath="Code" SelectionChanged="cbAction_SelectionChanged"> 
    <ComboBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Code}" Value="-"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
           <Separator HorizontalAlignment="Stretch" IsEnabled="False"/> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ComboBox.ItemContainerStyle> 
</ComboBox> 

除了我在這裏的問題和一個小的設計問題(我將提出另一個問題)之外,這個工作基本上很好。

使用鼠標時,用戶無法選擇分隔符,這是正確的。但是如果用戶使用向上/向下箭頭選擇項目,他們可以選擇分隔符。這不是默認行爲,它會跳過分隔符。

我怎樣才能使這個分離器的行爲類似的方式,如果你的XAML有各種ComboBoxItems和隔膜項目也將

回答

3

而不是設置「調焦」通過Meleak的建議,在二傳手「的IsEnabled」設置爲false來代替。

<DataTrigger Binding="{Binding Code}" Value="-"> 
    <Setter Property="IsEnabled" Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
       <Separator HorizontalAlignment="Stretch"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 
1

可選擇的項目是不是(使用上下鍵時跳過分隔符)分隔符本身,但包含分隔符的ComboBoxItem。
嘗試在DataTrigger中設置Focusable =「False」。這應該使ComboBoxItem「不可選擇」

更新
固定二傳位置

<DataTrigger Binding="{Binding Code}" Value="-"> 
    <Setter Property="Focusable" Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
       <Separator HorizontalAlignment="Stretch" IsEnabled="False"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 
+0

這確實解決了部分問題,但並不完全。如果我將組合框「展開」,則不再使用箭頭鍵選擇分隔符。但是如果組合框是當前製表位,如果您願意並且沒有展開,用戶可以使用箭頭鍵上下移動並仍然選擇分隔符。 – jmlumpkin 2010-11-24 14:12:01

+0

啊,是的,這是真的,我只想到下拉。要直接在ComboBox中進行選擇,您必須使用IsEnabled而不是像karmicpuppet所說的那樣使用Focusable。 – 2010-11-24 15:17:53

2

我嘗試了上面提到的建議,但仍然無法獲得分隔符。相反,它在組合框中添加了一個空白的可選條目。最後這是對我有用的。

我將綁定數據項設置爲NULL。而我的XAML看起來是這樣的:

<DataTrigger Binding="{Binding}" Value="{x:Null}"> 
    <Setter Property="IsEnabled" Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
       <Separator HorizontalAlignment="Stretch"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 
相關問題