Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值传递给IValueConverter_C#_Wpf_Listview_Mvvm_Ivalueconverter - Fatal编程技术网

C# 将值传递给IValueConverter

C# 将值传递给IValueConverter,c#,wpf,listview,mvvm,ivalueconverter,C#,Wpf,Listview,Mvvm,Ivalueconverter,我有一个列表视图,它有一个网格,有两列和多行。每行的每列中都有一个TextBlock,每个Text属性绑定到ListView的ItemSource中的一个值。我需要根据第一个TextBlock中的值对第二个TextBlock中的文本进行一些转换。如何向转换器获取第一个文本框的值 以下是我到目前为止的情况: XAML: <UserControl.Resources> <local:ValueStringConverter x:Key="valueStringConvert

我有一个
列表视图
,它有一个
网格
,有两列和多行。每行的每列中都有一个
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>

尝试多重绑定。您需要一个
IMultiValueConverter

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

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

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

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

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

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

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