2010-03-25 40 views
0

在我的項目中,我使用了一個ComboBox和我的自定義對象(如Employees)並使用DisplayMemberPath =「Description」。 這一切都像預期的完美。WPF組合框在樣式化後顯示對象類型

因爲此應用程序旨在與觸摸屏一起使用,所以我必須設計滾動條的樣式並使其更寬,這意味着我必須爲ScrolViewer創建一個新模板。這也相當不錯。

當我需要爲我需要的ComboBox創建一個新的ControlTemplate時,問題就出現了,因此我可以將ScrollViewerControlTemplate添加到ComboBox的ScrollViewer中。我使用Blend爲我生成ControlTemplate,然後將ScrollViewerControlTemplate綁定到ComboBox ControlTemplate中的ScrollViewer。

優秀,唯一的問題是ComboBox在控件的下拉部分內顯示正確的文本,但控件的ToggleButton部分顯示對象類型是[NameSpace] .Employee。

我試着改變ContentPresenter但沒有取得太大的成功。

<ContentPresenter 
        Content="{TemplateBinding SelectionBoxItem}" 
        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
        ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" 
        Margin="{TemplateBinding Padding}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 

任何幫助將不勝感激。

+0

我沒有時間來圖確切地說問題是什麼,但是我解決這個問題的方法是創建一個新窗口,將一個ComboBox拖到頁面上並再次生成Xaml,並將其全部複製到App.Xaml文件中。 我想我錯過了一些造成問題的風格。 – 2010-03-29 07:29:21

回答

1

在您的ComboBox模板中,該區域的內容應綁定到SelectionBoxItem屬性。所以,如果你使用的是ContentPresenter顯示選擇:如果您使用的是ToggleButton的內容區域

<ContentPresenter ContentSource="SelectionBoxItem" /> 

,你會做一些像這樣:

<ToggleButton Content="{TemplateBinding SelectionBoxItem}" 
       ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
       ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" /> 
+0

嘿菲拉,對不起,我不能把這個標記爲你的答案,因爲我已經改變並解決了問題,並且無法測試你的建議。 – 2010-03-29 07:32:48