2013-06-04 73 views
5

我有一個ListView有一個Grid兩列和許多行。每行在每列中都有一個TextBlock,每個Text屬性綁定到ListView的ItemSource中的值。我需要對第二個TextBlock中的文本進行一些轉換,具體取決於第一個TextBlock中的值。我怎樣才能得到第一個文本框的價值轉換器?傳遞值到IValueConverter

這是我到目前爲止有:

XAML:ValueStringConverter

<UserControl.Resources> 
    <local:ValueStringConverter x:Key="valueStringConverter" /> 
</UserControl.Resources> 

<ListView Name="theListView" ItemsSource="{Binding ItemCollection}" SelectedItem="{Binding SelectedItem}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Row="1" > 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 

       <TextBlock Text="{Binding Path=Key}" Grid.Column="0" Margin="0,0,10,0" /> 
       <TextBlock Text="{Binding Path=Value, Converter={StaticResource valueStringConverter}}" TextWrapping="Wrap" Grid.Column="1" /> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

代碼:

public class ValueStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string name = (string)value; 
     name = name.Replace("$$", " "); 
     name = name.Replace("*", ", "); 
     name = name.Replace("##", ", "); 

     return value; 
    } 

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

回答

5

可以多個值不傳遞到「常規」值轉換器。您可以使用IMultiValueConverter並將綁定定義爲MultiBinding

或者您可以創建一個IValueConverter,它接受DataContext中的整個對象,將該對象轉換爲其類型,取值和鍵並返回所需的字符串。

public class ValueStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     MyDataContextObjectType obj= (MyDataContextObjectType)value; 
     var name= obj.Name; 
     var key = obj.Key; 
     //here you have both Name and Key, build your string and return it 
     //if you don't know the type of object in the DataContext, you could get the Key and Name with reflection 
     name = name.Replace("$$", " "); 
     name = name.Replace("*", ", "); 
     name = name.Replace("##", ", "); 

     return value; 
    } 

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

儘量多結合。你需要一個IMultiValueConverter

public class MultiValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     var key = (string)values[0]; 
     var value = (string)values[1]; 

     // replace with appropriate logic 
     var result = key + ": " + value; 

     return result; 
    } 

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

,並略作修改XAML:

<TextBlock Text="{Binding Path=Key}" Grid.Column="0" Margin="0,0,10,0" /> 
<TextBlock TextWrapping="Wrap" Grid.Column="1"> 
    <TextBlock.Text> 
     <MultiBinding Converter={StaticResource valueStringConverter}> 
      <Binding Path="Key" /> 
      <Binding Path="Value" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 
0

綁定的實例,而不是(在這種情況下value)屬性:

關於第二個文本塊定義爲

<TextBlock Text="{Binding Converter={StaticResource valueStringConverter}"/> 

你轉換爲具有約束力。然後,您將可以訪問轉換器中的KeyValue

+0

> – Petar