2011-08-11 44 views
0

我正在嘗試創建一個自定義控件,其行爲有點像Expression Blend中工具箱中的「行」之一。WPF自定義「工具箱」選擇器控件

當關閉時,它顯示所述第一其物品,並且當用戶在其上持有鼠標按下爲一秒鐘左右,它「擴展」以顯示在一個彈出式中的其他項目不含這是目前的項目選中(點擊的項目仍然可見,但未與其他項目分組)。

我已經成功地製作了一個繼承自ContentControl的控件,它顯示一個項目,然後在展開時顯示其他項目,但當點擊一個項目時,首先顯示的項目不會改變。

模板如下:

<Border Background="{TemplateBinding Background}" 
BorderBrush="{TemplateBinding BorderBrush}" 
BorderThickness="{TemplateBinding BorderThickness}"> 
<Grid> 
    <ContentPresenter Content="{TemplateBinding MainItem}" /> // The item that is seen even when not expanded 
    <Popup Name="Popup" Placement="Right" IsOpen="{TemplateBinding IsExpanded}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade"> 
     <Border Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="0" > 
      <StackPanel Orientation="Horizontal" IsItemsHost="True" /> // The items only seen when expanded 
     </Border> 
    </Popup> 
</Grid> 

此刻,我必須手動設置在XAML的MainItem,這是不是ContentControlItems財產中的一員。

有沒有辦法來實現這一功能,所有的項目都是Items收集的一部分,在一個IsSelected財產或某項設置自動未顯示,然後被示出,其中顯示當前MainItem如果是這種情況?

我試圖將其更改爲一個Selector和使用上Items過濾器來實現這一點,結合第一ContentPresenterSelectedItem,但它似乎沒有工作。

如果可能,只有在控件展開時纔可見的項目順序應該與它們在XAML中的佈局順序相同,並且當前選定的項目缺失。

感謝

回答

0

嘗試使用ItemContainerStyle設置所選項目的知名度,「隱藏」。你需要一個BoolToVisibilityConverter(你可以隨便寫一個,或從WPFToolkit得到一個),以得到正確的值

<ContentPresenter Content="{Binding ElementName=selector, Path=SelectedItem}" /> 
<ComboBox x:Name="selector"> 
    <ComboBox.ItemContainerStyle> 
     <Style TargetType="ComboBoxItem"> 
      <Setter Property="Visibility" Value="{Binding Path=IsSelected, Converter={StaticResource boolToVisibilityConverter}}" /> 
     </Style> 
    </ComboBox.ItemContainerStyle> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding SomeProperty}" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox>ComboBoxItem 
+0

好的,謝謝 - 我用這個想法來隱藏哪個項目被選中,但是我無法在非擴展區域顯示該項目,因爲它無法同時連接到兩個父視覺效果。我想要的基本上是一個標準的ComboBox - 它能夠在非下拉區域顯示選定的項目。我認爲它在SelectionBoxItem模板中執行此操作... – user882807

0

我設法用VisualBrush創建項目的預覽來解決問題,而不必在視覺樹中移動它。