在WPF绑定中获取IValueConverter实现的ConvertBack()方法中的Source值

tom*_*tom 7 c# wpf binding ivalueconverter

我将依赖属性绑定到WPF中的textboxex.该属性是一个字符串,其中一些值以'/'分隔(例如:"1/2/3/4").我需要将单个值绑定到单独的文本框,这对以下Convert()方法的实现很好:

public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
  if (!string.IsNullOrEmpty(value as string))
  {
    String[] data = (value as string).Split('/');
    return data[Int16.Parse(parameter as string)];
  }
  return String.Empty;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用ConverterParameterin xaml来指定所需值的位置.然而,问题在于ConvertBack()方法.我不知道,如何获取源值,这样我就可以在字符串中添加或更改一个值(在指定位置).

谢谢你的帮助.

Fre*_*lad 14

更新

您可能已经在Vlad的帮助下解决了您的问题,我只是想我应该添加另一种方法来实际获取转换器中的源值.

首先,你可以使你的转换器派生,DependencyObject这样你就可以为它添加一个依赖属性,我们将绑定它

public class MyConverter : DependencyObject, IValueConverter
{
    public static DependencyProperty SourceValueProperty =
        DependencyProperty.Register("SourceValue",
                                    typeof(string),
                                    typeof(MyConverter));
    public string SourceValue
    {
        get { return (string)GetValue(SourceValueProperty); }
        set { SetValue(SourceValueProperty, value); }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //...
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object targetValue = value;
        object sourceValue = SourceValue;
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,转换器没有DataContext这样,Binding不会开箱即用,但你可以使用Josh Smith的优秀DataContextSpy:WPF中的人工继承上下文

<TextBox>
    <TextBox.Resources>
        <src:DataContextSpy x:Key="dataContextSpy" />
    </TextBox.Resources>
    <TextBox.Text>
        <Binding Path="YourProperty"
                 ConverterParameter="1">
            <Binding.Converter>
                <src:MyConverter SourceValue="{Binding Source={StaticResource dataContextSpy},
                                                       Path=DataContext.YourProperty}"/>
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

更新结束

Dr.WPF对此有一个优雅的解决方案,请参阅以下线程
在ConvertBack()中访问绑定源的方法?

编辑

使用Dr.WPF的解决方案,您可以TextBox使用此(可能有点详细)示例代码为转换器提供字符串索引和源代码

<TextBox dw:ObjectReference.Declaration="{dw:ObjectReference textBoxSource}">
    <TextBox.Text>
        <Binding Path="YourStringProperty"
                 Converter="{StaticResource YourConverter}">
            <Binding.ConverterParameter>
                <x:Array Type="sys:Object">
                    <sys:Int16>1</sys:Int16>
                    <dw:ObjectReference Key="textBoxSource"/>
                </x:Array>
            </Binding.ConverterParameter>
        </Binding>
    </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

然后您可以稍后访问索引和TextBoxConvertBack方法

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    object[] parameters = parameter as object[];
    short index = (short)parameters[0];
    object source = (parameters[1] as TextBox).DataContext;
    //...
}
Run Code Online (Sandbox Code Playgroud)


Vla*_*lad 5

在大多数情况下,你可以放心地ConvertBack投掷NotImplementedException.

实际上,您还没有获得足够的信息来重新创建源代码!

如果你真的需要反向转换(例如,如果你使用双向绑定),我会在视图模型(使用的类DataContext)中将属性拆分为3个字符串,并分别绑定它们.

  • 它不应抛出`NotImplementedException`而是抛出`NotSupportedException`,因为该方法永远不会实现. (8认同)