传递价值观的IValueConverter [英] Passing values to IValueConverter

查看:181
本文介绍了传递价值观的IValueConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 具有电网有两列和多行。每一行都有一个的TextBlock 在每列绑定到ListView控件的值文本属性的ItemSource 。我需要的TextBlock 根据在第一个的TextBlock 值做文本的某些转换在第二位。我怎样才能获得的第一个文本框的值的转换器?

下面是我到目前为止有:

XAML:

 < UserControl.Resources>
    <局部:ValueStringConverter X:键=valueStringConverter/>
< /UserControl.Resources>< ListView控件名称=theListView的ItemsSource ={结合ItemCollection}的SelectedItem ={结合的SelectedItem}ScrollViewer.Horizo​​ntalScrollBarVisibility =已禁用Grid.Row =1>
    < ListView.ItemTemplate>
        <&DataTemplate的GT;
            <网格和GT;
                < Grid.RowDefinitions>
                    < RowDefinition高度=自动/>
                < /Grid.RowDefinitions>                < Grid.ColumnDefinitions>
                    < ColumnDefinition WIDTH =自动/>
                    &所述; ColumnDefinition宽度=*/>
                < /Grid.ColumnDefinitions>                < TextBlock的文本={绑定路径=关键}Grid.Column =0利润=0,0,10,0/>
                < TextBlock的文本={绑定路径=时,转换器= {StaticResource的valueStringConverter}}TextWrapping =包装Grid.Column =1/>
            < /网格和GT;
        < / DataTemplate中>
    < /ListView.ItemTemplate>
< /&的ListView GT;

code ValueStringConverter

 公共类ValueStringConverter:的IValueConverter
{
    公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        字符串名称=(字符串)值;
        名称= name.Replace($$,);
        名称= name.Replace(*,,);
        名称= name.Replace(##,,);        返回值;
    }    公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        抛出新NotImplementedException();
    }
}


解决方案

您不能超过一个值传递给正规军的价值转换器。你可以使用 IMultiValueConverter 去定义绑定作为 MultiBinding

或者你可以创建一个的IValueConverter这需要在DataContext的整个对象,把对象到它的类型,拿这个值键和返回你需要的字符串。

在你的第二个文本块定义为

绑定

 < TextBlock的文本={结合转换器= {StaticResource的valueStringConverter}/>

和您的转换器:

 公共类ValueStringConverter:的IValueConverter
{
    公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        MyDataContextObjectType OBJ =(MyDataContextObjectType)值;
        变量名称= obj.Name;
        VAR键= obj.Key;
        //这里你有两个名称和重点,建立自己的字符串,并将其返回
        //如果你不知道对象在DataContext的类型,你可以得到密钥和名称与反思
        名称= name.Replace($$,);
        名称= name.Replace(*,,);
        名称= name.Replace(##,,);        返回值;
    }    公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        抛出新NotImplementedException();
    }
}

I have a ListView that has a Grid with two columns and many rows. Each row has a TextBlock in each column with each Text property binded to a value in ListView's ItemSource. I need to do some converting of the text in the second TextBlock depending on the value in the first TextBlock. How can I get the value of the first text box to the converter?

Here is what I have so far:

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>

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

解决方案

You can't pass more than one value to the "regular" value converter. You could go with IMultiValueConverter and define the binding as MultiBinding.

Or you can create a IValueConverter that takes the entire object in the DataContext, cast the object to its type, take the Value and Key and return the string you need.

On your second textblock define the binding as

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

And your converter as:

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

这篇关于传递价值观的IValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆