将值传递给IValueConverter

Chr*_*ris 5 c# wpf listview mvvm ivalueconverter

我有一个ListViewGrid两列和多行.每一行在每列中都有一个绑定到ListView中的值的TextBlock每个Text属性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>
Run Code Online (Sandbox Code Playgroud)

代码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();
    }
}
Run Code Online (Sandbox Code Playgroud)

jur*_*ure 6

您不能将多个值传递给"常规"值转换器.您可以使用IMul​​tiValueConverter并将绑定定义为MultiBinding.

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

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

<TextBlock Text="{Binding Converter={StaticResource valueStringConverter}"/>
Run Code Online (Sandbox Code Playgroud)

你的转换器为:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)


max*_*max 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并稍加修改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>
Run Code Online (Sandbox Code Playgroud)