2013-05-17 16 views
0

我正在顯示來自包含「狀態」列的表的數據,現在此列包含兩個值0和1 0 =>每日 1 =>每月 通過使用mvvm結構,當我綁定我的單元格文本屬性到該表的返回集合時,它顯示0和1. 我想要的是而不是0,每日和每月1應顯示。 有沒有辦法實現這?如何使用mvvm顯示datagrid單元格中int值的文本

回答

1

是的,你可以通過實現接口IValueConverter創建綁定轉換器。

public class IntTextConverter : IValueConverter 
{ 
    // This converts the int object to the string 
    // to display 0 => Daily other values => Monthly. 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     // You can test type an value (0 or 1) and throw exception if 
     // not in range or type 
     var intValue = (int)value; 
     // 0 => Daily 1 => Monthly 
     return intValue == 0 ? "Daily" : "Monthly"; 
    } 

    // No need to implement converting back on a one-way binding 
    // but if you want two way 
    public object ConvertBack(object value, Type targetType, 
     object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value == "Daily" ? 0 : 1; 
    } 
} 

而在XAML中,樣品在文本塊結合:感謝名單@Tonio

<Grid.Resources> 
    <local:IntTextConverter x:Key="IntTextConverter" /> 
</Grid.Resources> 
... 
<TextBlock Text="{Binding Path=Status, Mode=OneWay, 
      Converter={StaticResource IntTextConverter}}" /> 
+0

,它的工作原理 我有同樣的事情相關的另一個問題,應該更新我的問題,或者我可以從你來的? – zeeshan

+0

thanx一個男人,你的答案是如此完整! – zeeshan

+0

不客氣;)。如果它的主題是「如何使用mvvm顯示datagrid單元格中的int值的文本」,則還要創建一個新的。 – Tonio

相關問題