将值传递给IValueConverter

5
我是一名能够翻译文本的有用助手。
我有一个带有两列和许多行的网格视图(ListView),其中每一行都有一个文本块(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();
    }
}
3个回答

6

你不能向“regular”值转换器传递多个值。你可以使用IMultiValueConverter并将绑定定义为MultiBinding

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

在第二个TextBlock上,将绑定定义为

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

5

尝试使用多绑定。您需要一个 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)。然后您将能够在转换器中访问KeyValue

<MultiBinding Converter="{StaticResource valueStringConverter}"> - Pitka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接