【问题标题】:WPF Multibinding: OneWayToSource binding from TextBox updated via another binding doesnt work?WPF Multibinding:来自 TextBox 的 OneWayToSource 绑定通过另一个绑定更新不起作用?
【发布时间】:2016-09-16 23:47:09
【问题描述】:

我有一个绑定到 People 集合的 DataGrid。我还有一个 TextBox 应该接受来自所选行的 Name 值。然后,用户可以编辑该值或保持原样。关键点是:TextBox 中显示的文本无论是来自集合还是用户输入,都必须传播到属性 NewName。

我为 NewNameTextBox 设置了两个绑定:OneWay'ed 到 DataGrid 后面的 CollectionView,OneWayToSource'ed 到属性:

<Window.Resources>
    <CollectionViewSource x:Key="PeopleCollection" 
         Source="{Binding Path=People, Mode=OneWay}" />
    <local:ConverterNewNamePrefill x:Key="ConverterNewNamePrefill" />
</Window.Resources>
<Grid>
    <StackPanel>
        <DataGrid ItemsSource="{Binding Source={StaticResource PeopleCollection}}"
                  AutoGenerateColumns="True"
                  IsReadOnly="True"
                  Margin="10">
        </DataGrid>
        <StackPanel Orientation="Horizontal" Margin="10">
            <TextBox>
                <TextBox.Text>
                    <MultiBinding Converter="{StaticResource ConverterNewNamePrefill}" >
                        <Binding Source="{StaticResource PeopleCollection}" Path="Name" Mode="OneWay" />
                        <Binding Path="NewName" Mode="OneWayToSource" />
                    </MultiBinding>
                </TextBox.Text>
            </TextBox>
        </StackPanel>
    </StackPanel>
</Grid>

我想当用户更改 DataGrid 中的选择时应该更新该属性,但这不会发生。 TextBox 得到更新并显示选定的 Name 值,但通过 OneWayToSource 绑定的属性保持不变。

如果用户在 TextBox 中键入内容,则该属性会按预期更新。

所以问题是如何通过多绑定 TextBox 从两个来源更新属性,而无需代码隐藏视图?

下面是window后面的代码:

public class Person
{
        public string Name { get; set; }
        public string Surname { get; set; }
}

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

    private ObservableCollection<Person> _people = new ObservableCollection<Person> {
        new Person() {Name = "Mitchell", Surname = "Sofia" },
        new Person() {Name="Bush", Surname="Ethan" },
        new Person() {Name="Ferrero", Surname="Emma" },
        new Person() {Name="Thompson", Surname="Aiden" }
    };
    public ObservableCollection<Person> People => _people;

    public string NewName { get; set; } = "Jackson";
}

public class ConverterNewNamePrefill : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0]; 
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return new[] { value, value };
    }
}

转换器的方法 ConvertBack() 仅在用户键入时调用,而不是在 TextBox.Text 从集合中更新时调用。

谢谢!

【问题讨论】:

    标签: c# wpf xaml multibinding imultivalueconverter


    【解决方案1】:

    这就是绑定的工作方式。除非目标通过绑定本身以外的方式更改,否则不会更新源或源。 IE。假设如果目标刚刚从源更新,那么源已经是最新的并且不需要更新。

    如果没有更多详细信息,就很难确定您想要什么。但似乎您可能希望NewName 实际上成为第二个绑定的目标,其中源与Name 属性相同,用作TextBox.Text 属性的源,或者您想订阅TextBox.TextChanged 事件,并且您的处理程序在引发该事件时将值显式写回NewName 属性。

    在前一种情况下,您必须将NewName 设为MainWindow 的依赖属性。这是您可能想也可能不想处理的并发症。如果没有,那么我会推荐后一种方法。

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 2014-08-27
      • 2011-12-14
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多