[英]How to get in ConvertBack parameters from Convert?
I am using MultiBinding
with converter to pass (x,y)
coordinates into method. 我正在使用MultiBinding
与转换器将(x,y)
坐标传递给方法。
And I can't make it working in back direction: 而且我无法使其反向运行:
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var x = (int)values[0];
var y = (int)values[1];
return Model.Get(x, y);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Model.Set(x, y, value); // how to get x, y here?
return new object[] { Binding.DoNothing, Binding.DoNothing };
}
}
The data will be visualized in a form of table. 数据将以表格形式显示。 Here is cell template: 这是单元格模板:
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource converter}" Mode="TwoWay">
<Binding Path="X" Mode="OneWay" />
<Binding Path="Y" Mode="OneWay" RelativeSource="..." />
</MultiBinding>
</TextBox.Text>
</TextBox>
The idea is to use converter, which receive x
(from cell view model) and y
(from parent column view model, notice RelativeSource
) and calls Get(x,y)
to display value. 这个想法是使用转换器,它接收x
(从单元格视图模型)和y
(从父列视图模型,请注意RelativeSource
)并调用Get(x,y)
以显示值。
However, when user entered something, ConvertBack
is called and I need to call Set(x, y, value)
method. 但是,当用户输入内容时,将调用ConvertBack
,而我需要调用Set(x, y, value)
方法。
How do I pass x
and y
into ConvertBack
? 如何将x
和y
传递到ConvertBack
?
There might be more-or-less dirty workarounds to get such a multivalue converter working. 为了使这样的多值转换器正常工作,可能存在或多或少的肮脏解决方法。 But I'd suggest you keep your multivalue converter one-way, but return a container object that wraps the actual text property. 但是我建议您将多值转换器保持单向,但是返回一个包装实际text属性的容器对象。
Instead of directly binding to the TextBox.Text
property, bind to some other property (eg. DataContext
or Tag
) and then bind the text to the container value. 而不是直接绑定到TextBox.Text
属性,而是绑定到其他属性(例如DataContext
或Tag
),然后将文本绑定到容器值。
Small example: 小例子:
<TextBox Text="{Binding Value}">
<TextBox.DataContext>
<MultiBinding Converter="{StaticResource cMyConverter}">
<Binding Path="X"/>
<Binding Path="Y"/>
</MultiBinding>
</TextBox.DataContext>
</TextBox>
With container and converter: 带有容器和转换器:
public class ValueProxy
{
public int X { get; set; }
public int Y { get; set; }
public string Value
{
get { return Model.Get(X, Y); }
set { Model.Set(X, Y, value); }
}
}
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var x = (int)values[0];
var y = (int)values[1];
return new ValueProxy { X = x, Y = y };
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { Binding.DoNothing, Binding.DoNothing };
}
}
The short answer is that you can't directly get the values of x
and y
inside your ConvertBack
method. 简短的答案是,您不能直接在ConvertBack
方法中获取x
和y
的值。 The IMultiValueConverter
Convert
multiple values into a single value. IMultiValueConverter
Convert
多个值转换为单个值。 So, the ConvertBack
method will do the opposite: convert a single value into multiple values. 因此, ConvertBack
方法将执行相反的操作:将单个值转换为多个值。
It all depends on what your Model.Get(x, y) method returns. 这完全取决于您的Model.Get(x,y)方法返回什么。 It needs to return a value that is unique enough for you to get the separate values of x
and y
from it. 它需要返回一个足够唯一的值,以便您从中获取x
和y
的单独值。
Example: create unique strings for each pair of (x,y). 示例:为每对(x,y)创建唯一的字符串。
It seems hard to pass parameters into ConvertBack
. 似乎很难将参数传递给ConvertBack
。 It might be possible, but there is a workaround, which makes ConvertBack
unnecessary. 有可能,但是有一种解决方法,这使ConvertBack
变得不必要。 Thanks to @XAMlMAX for an idea. 感谢@XAMlMAX的想法。
One possibility to achieve it (there could be a better way) is to use data templates. 实现它的一种可能性(可能有更好的方法)是使用数据模板。 Instead of multi-binding TextBlock.Text
with string
we can bind ContentControl.Content
with some viewmodel, and this viewmodel should do the rest, including Set(x, y, value)
call. 代替使用string
多绑定TextBlock.Text
,我们可以将ContentControl.Content
与某些视图模型绑定,该视图模型应完成其余工作,包括Set(x, y, value)
调用。
Here is code: 这是代码:
public class ViewModel
{
public int X { get; set; }
public int Y { get; set; }
string _text;
public string Text
{
get { return _text; }
set
{
// this should be only called by the view
_text = value;
Model.Set(X, Y, value);
}
}
public ViewModel(string text)
{
_text = text;
}
}
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var x = (int)values[0];
var y = (int)values[1];
return new ViewModel(Model.Get(x, y)) { X = x, Y = y };
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and the xaml will become xaml将变为
<ContentControl Focusable="False">
<ContentControl.Content>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="X" />
<Binding Path="Y" RelativeSource="..."/>
</MultiBinding>
</ContentControl.Content>
</ContentControl>
where data template is 数据模板在哪里
<DataTemplate DataType="{x:Type local:ViewModel}">
<TextBox Text="{Binding Text}" />
</DataTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.