我剛剛注意到一個看起來像一個bug的奇怪行爲。請看下面的XAML:ComboBox.Text沒有考慮到ItemStringFormat屬性
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Page.Resources>
<x:Array x:Key="data" Type="{x:Type sys:String}">
<sys:String>Foo</sys:String>
<sys:String>Bar</sys:String>
<sys:String>Baz</sys:String>
</x:Array>
</Page.Resources>
<StackPanel Orientation="Vertical">
<Button>Boo</Button>
<ComboBox Name="combo" ItemsSource="{Binding Source={StaticResource data}}" ItemStringFormat="##{0}##" />
<TextBlock Text="{Binding Text, ElementName=combo}"/>
</StackPanel>
</Page>
的ComboBox
顯示的數值爲 「## ##美孚」, 「##條##」 和 「##巴茲##」。但TextBlock
顯示所選值爲「Foo」,「Bar」和「Baz」。所以ItemStringFormat
明顯被忽略的Text
屬性...
這是一個錯誤?如果是,是否有解決方法?
或者我只是做錯了什麼?
感謝您的回答。但是我的問題有點複雜......我需要在ViewModel中檢索ComboBox的文本,所以我使用Mode = OneWayToSource進行綁定。我使用的ItemStringFormat是在包含模板的程序集資源中定義的,但ViewModel在另一個程序集中,並且無法訪問此資源...因此,我將不得不在ViewModel程序集中複製此資源。我不喜歡那樣,但至少它會起作用...... – 2010-05-17 09:51:45