2009-06-19 43 views

回答

1

您是否想找.SelectionBoxItemTemplate

+0

是,類似的東西,但SL3 ComboBox似乎沒有該屬性? – user112889 2009-06-19 03:46:23

+0

奇怪...我沒有SL3在這裏測試,但它已列在MSDN文檔中(即使SL3未列在「其他版本」框中:在.aspx之前添加(VS.96)我的鏈接)。也許這是測試版的問題?嘗試忽略智能感知,如果這就是你要做的,並且無論如何只需編譯它。 – 2009-06-19 04:01:36

+0

唉......我試過了,得到這個:XamlParseException。 AG_E_UNKNOWN_ERROR [Line:96 Position:135]是的,第96行是SelectionBoxItemTemplate的行;-) – user112889 2009-06-21 20:16:23

0

SelectionBoxItemTemplate在Silverlight4中可用,但無法從代碼隱藏設置此屬性的值,因爲它是隻讀屬性。它也不是依賴項屬性,因此無法使用comboBox.SetValue()方法設置值。有關如何在代碼隱藏中爲該屬性賦值的任何想法?

3

您可以通過創建自己的SelectionBoxItemTemplate附加屬性,然後爲在選擇框區域的內容展示器中使用該模板的組合框定義新樣式/控件模板。

這裏有一個合適的附加屬性:

public class ComboBoxExt 
{ 
    public static DataTemplate GetSelectionBoxItemTemplate(DependencyObject obj) 
    { 
     return (DataTemplate) obj.GetValue(SelectionBoxItemTemplateProperty); 
    } 

    public static void SetSelectionBoxItemTemplate(DependencyObject obj, DataTemplate value) 
    { 
     obj.SetValue(SelectionBoxItemTemplateProperty, value); 
    } 

    public static readonly DependencyProperty SelectionBoxItemTemplateProperty = 
     DependencyProperty.RegisterAttached("SelectionBoxItemTemplate", typeof (DataTemplate), typeof (ComboBoxExt), 
              new PropertyMetadata(null)); 

} 

要更新ComboBox控件模板,尋找名爲ContentPresenter裏面一個叫ContentPresenterBorder(你可以找到組合框here默認樣式)的元素。您需要刪除ContentPresenter的名稱(否則ComboBox將通過代碼顯式地爲其屬性設置值,而忽略您設置的數據綁定)。

下面是經調節的控制模板中的ContentPresenter元素應該是什麼樣子:

<ContentPresenter Margin="{TemplateBinding Padding}" 
        Content="{Binding Path=SelectedItem, RelativeSource={RelativeSource TemplatedParent}}" 
        ContentTemplate="{Binding (a:ComboBoxExt.SelectionBoxItemTemplate), RelativeSource={RelativeSource TemplatedParent}}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
</ContentPresenter> 

最後,要利用這一點,你會做這樣的事情:

<ComboBox 
    Style="{StaticResource MyAdjustedComboBoxStyle}" 
    ItemTemplate="{StaticResource MyDropDownAreaTemplate}" 
    Behaviors:ComboBoxExt.SelectionBoxItemTemplate="{StaticResource MySelectionAreaTemplate}"> 
+0

此解決方案值得+50。我剛剛在一個SL項目中遇到了這個問題,認爲這是一個很長的鏡頭,但它的工作方式就像一個魅力! – 2014-06-03 05:22:41

相關問題