绑定到验证规则中绑定对象的不同属性

给出以下视图模型示例

public class MyViewModel
{
  public ObservableCollection<myobjType> BoundItems { get; }
}

myobjType

public class myobjType
{
  public string Name { get; set; }
  public int Id { get; set; }
}

我已将验证规则添加到DataGrid列,其中DataGrid绑定到ViewModel中的BoundItems集合,而Template列中的Text属性绑定到Name。

<DataGrid ItemsSource="{Binding BoundItems}">
      <DataGrid.Columns>
             <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TexBox>
                          <TextBox.Text>
                            <Binding Path="Name" ValidatesOnDataErrors="True">
                              <Binding.ValidationRules>
                                <xns:MyValidationRule>
                                  <xns:MyValidationRule.SomeDependencyProp>
                                    <xns:SomeDependencyProp SubProp={Binding Id} /> <!-- Not Working -->
                                  </xns:MyValidationRule.SomeDependencyProp>
                                </xns:MyValidationRule>
                              </Binding.ValidationRules>
                            </Binding>
                          </TextBox.Text>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
      </DataGrid.Columns>
</DataGrid>

我想将我的集合类型(Id)的另一个属性myobjType传递给验证规则,如何从规则中访问该属性。我知道关于freezable以及获取视图模型的上下文的信息,但是我需要绑定到Datagrid的集合类型的另一个属性。

ValidationRule和SomeDependencyProp是根据此处的示例建模的:https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx

public class SomeDependencyProp : DependencyObject
{
  public static readonly SubPropProperty =
     DependencyProperty.Register("SubProp",typeof(int),typeof(SomeDependencyProp),new FrameworkPropertyMetadata(0));

  public int SubProp{
    get { return (int)Getvalue(SubPropProperty ); }
    set { Setvalue(SubPropProperty,value); }
  }
}

public class MyValidationRule: System.Windows.Controls.ValidationRule
{
  public override ValidationResult Validate(object value,CultureInfo cultureInfo) 
  {
    ...
  }

  public SomeDependencyProp SomeDependencyProp { get; set; }
}
wan_XM 回答:绑定到验证规则中绑定对象的不同属性

这是在运行时在VS的“输出”窗格中查看时会看到的错误:

  

找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path = Id; DataItem = null;目标元素是'SomeDependencyProp'(HashCode = 11898202);目标属性是“ SubProp”(类型“ Int32”)

将跟踪添加到绑定({Binding Id,PresentationTraceSources.TraceLevel=High})中,您将看到很多有关“找不到框架导师”的讨论。这里的问题是验证规则在可视树之外,因此任何父对象都不会继承DataContext(因此,上面的错误消息中的DataItem = null)。 {Binding Id}失败,因为没有来源;它不知道去哪里寻找Id属性。

这种情况的典型解决方案是使用BindingProxy。编写绑定代理类的规范方法是在Google上搜索“ stackoverflow bindingproxy wpf”或类似内容,然后窃取the first one you find

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty,value); }
    }

    // Using a DependencyProperty as the backing store for Data.  
    // This enables animation,styling,binding,etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data",typeof(object),typeof(BindingProxy),new UIPropertyMetadata(null));
}

然后像这样使用:

<TextBox>
    <TextBox.Resources>
        <local:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
    </TextBox.Resources>
    <TextBox.Text>
        <Binding Path="Name" ValidatesOnDataErrors="True">
            <Binding.ValidationRules>
                <xns:MyValidationRule>
                    <xns:MyValidationRule.SomeDependencyProp>
                        <xns:SomeDependencyProp 
                            SubProp="{Binding Data.Id,Source={StaticResource DataContextProxy}}" 
                            />
                    </xns:MyValidationRule.SomeDependencyProp>
                </xns:MyValidationRule>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

如果在DataTemplate.Resources中而不是TextBox.Resources中创建BindingProxy,您将再次得到“找不到框架导师”。以上为我工作。

本文链接:https://www.f2er.com/3159020.html

大家都在问