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

标签 c# wpf binding ivalueconverter

我正在将依赖属性绑定(bind)到 WPF 中的 textboxex。该属性是一个字符串,其中包含一些由“/”分隔的值(例如:“1/2/3/4”)。我需要将各个值绑定(bind)到单独的文本框,这对于以下 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;
}

我在 xaml 中使用 ConverterParameter 来指定所需值的位置。 但是,问题出在 ConvertBack() 方法上。我不知道如何获取源值,以便我可以只添加或更改字符串中的一个值(在指定位置)。

感谢您的帮助。

最佳答案

更新

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

首先,您可以使您的转换器派生自 DependencyObject,这样您就可以向它添加一个我们将绑定(bind)到的依赖属性

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;
        //...
    }
}

不幸的是,Converter 没有DataContext,因此Binding 无法开箱即用,但您可以使用Josh Smith 出色的DataContextSpy:Artificial Inheritance Contexts in 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>

更新结束

Dr.WPF 对此有一个优雅的解决方案,请参阅以下线程
The way to access binding source in 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>

然后您可以稍后在 ConvertBack 方法中访问索引和 TextBox

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;
    //...
}

关于c# - 在 WPF 绑定(bind)中获取 IValueConverter 实现的 ConvertBack() 方法中的 Source 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7064445/

相关文章:

c# - 在哪里可以找到默认的 wpf 图表模板?

c# - 模块类型加载异常 : Failed to load type for module

android - 绑定(bind)到 jar 文件中的服务

c# - 仍然对协变和逆变以及输入/输出感到困惑

c# - .Net 中的文件操作是否有异步替代方案?

c# - 如何让gridview中的一个数据字段成为一个url

c++ - 如何将 C++ 函数绑定(bind)到 xaml 文本文本 block 属性

wpf - MVVM DataTemplate 绑定(bind)问题

c# - 如何在C#中获取Azure表存储中的所有行?

c# - 包含创建实例的方法的接口(interface)