【问题标题】:Caliburn Micro: ElementName for ComboBox in DataGrid not workingCaliburn Micro:DataGrid 中 ComboBox 的 ElementName 不起作用
【发布时间】:2012-05-30 07:33:41
【问题描述】:

我的问题是 DataGrid 中 ComboBox 的 ItemsSource 没有绑定。 我的用户控件:

<UserControl x:Class="MyClass"
             x:Name="Root"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
             mc:Ignorable="d" 
             d:DesignHeight="360" d:DesignWidth="757">

数据网格:

  <DataGrid x:Name="Article" 
              AutoGenerateColumns="False"
              SelectionUnit="FullRow" 
              SelectionMode="Single"
              CanUserAddRows="False" 
              CanUserDeleteRows="True"
              Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4"
              Margin="5">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="*"
                                    Header="Article">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding FilteredArticle, ElementName=Root}"                                      IsEditable="True" 
                                  cal:Message.Attach="[Event KeyUp] = [Action FindArticlel($source)]" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

如果我不说 ElementName=Root,他会尝试在 Article 类中绑定 FilteredArticle。 如果我说 ElementName=Root,他在运行时会说以下内容:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Root'. BindingExpression:Path=FilteredArticle; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

FilteredArticle 是我的 ViewModel 中的 BindableCollection。所有其他绑定都在工作。 这里有什么问题? 使用最新稳定版本的 Caliburn.Micro。

【问题讨论】:

    标签: wpf xaml combobox caliburn.micro elementname


    【解决方案1】:

    通过ElementName 绑定到视图通常是一种不好的做法。主要是因为创建视图实例的人可以给它一个不同的x:Name,这会破坏你的内部绑定。

    另外,FilteredArticle 属性不是视图的属性,而是视图模型的属性,它是视图的数据上下文。

    在这些场景中使用相对源进行绑定

    ItemsSource="{Binding DataContext.FilteredArticle, 
                          RelativeSource={RelativeSource FindAncestor, 
                                                 AncestorType={x:Type UserControl}}}"
    

    您可以使用更具体的符号(尽管在 99% 的情况下没有必要)

    ItemsSource="{Binding DataContext.FilteredArticle, 
                          RelativeSource={RelativeSource FindAncestor, 
                                                 AncestorType={x:Type local:MyClass}}}"
    

    其中localxmlns 的命名空间MyClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2011-12-23
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多