2010-05-17 79 views
2

我剛剛注意到一個看起來像一個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屬性...

這是一個錯誤?如果是,是否有解決方法?
或者我只是做錯了什麼?

回答

1

這不是一個錯誤:ItemStringFormat只是一個「數據模板的文本塊綁定到綁定中設置的指定字符串格式的值」的快捷方式。 Text但通常在IsEditable爲真時表示用戶輸入。如果列表中沒有任何字符串,最好使用SelectedItem而不是Text。在任何情況下,下面的代碼將重新格式化的文字:

<TextBlock Text="{Binding ElementName=combo, Path=Text, StringFormat='##{0}##'}"/> 
+0

感謝您的回答。但是我的問題有點複雜......我需要在ViewModel中檢索ComboBox的文本,所以我使用Mode = OneWayToSource進行綁定。我使用的ItemStringFormat是在包含模板的程序集資源中定義的,但ViewModel在另一個程序集中,並且無法訪問此資源...因此,我將不得不在ViewModel程序集中複製此資源。我不喜歡那樣,但至少它會起作用...... – 2010-05-17 09:51:45

0

我知道這可能是爲時已晚,以幫助OP,但是碰到這個以防萬一別人絆倒......

我將使用解決方案來解決OP在其他答案的評論中提到的實際問題,即使用IValueConverter

這裏是爲FormatConverter類的代碼:

public class FormatConverter : System.Windows.Data.IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string format = parameter.ToString(); 
     return string.Format(format, value); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

這裏是你如何使用它(從修改問題採取的):

<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" 
    xmlns:local="clr-namespace:YourNamespace"> 
    <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> 
    <local:FormatConverter x:Key="FormatConverter" /> 
    </Page.Resources> 
    <StackPanel> 
    <ComboBox ItemsSource="{Binding Source={StaticResource data}}" ItemStringFormat="##{0}##" 
       Text="{Binding Path=VMProp, Mode=OneWayToSource, Converter={StaticResource FormatConverter}, ConverterParameter=##{0}##}" /> 
    </StackPanel> 
</Page> 

這將導致項目出現感謝將ItemStringFormat設置爲「## {0} ##」,將ComboBox設置爲「## Foo ##」,「## Bar ##」和「## Baz ##」。此外,由於FormatConverterConverterParameter被設置爲「## {0} ##」,因此ViewModel上的VMProp屬性將被選爲相同格式的值。

請注意,即使我使用ComboBox.Text屬性來保持與原始問題一致,我會建議ComboBox.SelectedItem屬性會更合適。 ;)