wpf 将值传递给 IValueConverter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16926709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing values to IValueConverter
提问by Chris
I have a ListViewthat has a Gridwith two columns and many rows. Each row has a TextBlockin each column with each Textproperty binded to a value in ListView's ItemSource. I need to do some converting of the text in the second TextBlockdepending on the value in the first TextBlock. How can I get the value of the first text box to the converter?
我有一个ListView有Grid两列多行的。每行在每一列中都有一个TextBlock,每个Text属性都绑定到 ListView 中的一个值ItemSource。我需要TextBlock根据第一个中的值对第二个中的文本进行一些转换TextBlock。如何获取转换器的第一个文本框的值?
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();
}
}
回答by jure
You can't pass more than one value to the "regular" value converter. You could go with IMultiValueConverterand 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();
}
}
回答by max
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>
回答by trydis
Bind to the instance, not the property (valuein this case). Then you will have access to both Keyand Valuein the converter.
绑定到实例,而不是属性(value在本例中)。然后,您将可以在转换器中访问Key和Value。

