[英]Passing values to IValueConverter
I have a ListView
that has a Grid
with two columns and many rows. 我有一个
ListView
,它有一个包含两列和多行的Grid
。 Each row has a TextBlock
in each column with each Text
property binded to a value in ListView's ItemSource
. 每行在每列中都有一个
TextBlock
,每个Text
属性绑定到ListView的ItemSource
的值。 I need to do some converting of the text in the second TextBlock
depending on the value in the first TextBlock
. 我需要根据第一个
TextBlock
的值对第二个TextBlock
中的文本进行一些转换。 How can I get the value of the first text box to the converter? 如何将第一个文本框的值输入转换器?
Here is what I have so far: 这是我到目前为止:
XAML: 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
: 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 .
您可以使用IMultiValueConverter并将绑定定义为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. 或者,您可以创建一个IValueConverter,它接受DataContext中的整个对象,将对象强制转换为其类型,获取Value和Key并返回所需的字符串。
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();
}
}
Try multi-binding. 尝试多重绑定。 You'll need an
IMultiValueConverter
: 你需要一个
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();
}
}
and slightly modified XAML: 并稍加修改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>
Bind to the instance, not the property ( value
in this case). 绑定到实例,而不是属性(在这种情况下为
value
)。 Then you will have access to both Key
and Value
in the converter. 然后,您将可以访问转换器中的
Key
和Value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.