【问题标题】:Passing values to IValueConverter将值传递给 IValueConverter
【发布时间】:2013-06-04 20:18:29
【问题描述】:

我有一个ListView,它有一个Grid,有两列和多行。每行的每一列都有一个TextBlock,每个Text 属性都绑定到ListView 的ItemSource 中的一个值。我需要根据第一个TextBlock 中的值对第二个TextBlock 中的文本进行一些转换。如何将第一个文本框的值传递给转换器?

这是我目前所拥有的:

XAML:

<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>

ValueStringConverter的代码:

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();
    }
}

【问题讨论】:

    标签: c# wpf listview mvvm ivalueconverter


    【解决方案1】:

    您不能将多个值传递给“常规”值转换器。您可以使用IMultiValueConverter 并将绑定定义为MultiBinding

    或者您可以创建一个 IValueConverter,它获取 DataContext 中的整个对象,将对象转换为其类型,获取 Value 和 Key 并返回您需要的字符串。

    在您的第二个文本块上将绑定定义为

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

    你的转换器为:

    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试多重绑定。你需要一个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>
      

      【讨论】:

        【解决方案3】:

        绑定到实例,而不是属性(在本例中为value)。然后您将可以在转换器中访问KeyValue

        【讨论】:

        猜你喜欢
        • 2021-08-14
        • 1970-01-01
        • 2016-09-24
        • 2015-12-09
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多