c# - 用户控件作为具有绑定(bind)的数据模板

标签 c# wpf mvvm user-controls datatemplate

我有一个 ItemsControl,其中 ItemsSource 绑定(bind)到 SystemModels 列表。它必须为列表中的每个系统生成一个用户控件。在这些用户控件中,它有一些文本框,显示系统的名称、位置和位置。

我的代码创建了用户控件,但没有填充用户控件中的文本框。

查看:

<UserControl x:Name="SystemListScreen">
        <ScrollViewer Grid.Row="1">
                <ItemsControl x:Name="SystemList" ItemsSource="{Binding Path=Systems}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="4"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate DataType="SystemModel">
                        <Widgets:MultiLineButton partID="{Binding ID}"
                                                    company="{Binding ItemsSource.Company}"
                                                    typeSorter="{Binding ItemsSource.Name, ElementName=SystemList}"
                                                    typeLocation="{Binding ItemsSource.Location, ElementName=SystemList}"
                                                    buttonCommand="{Binding DataContext.navigateInspectList, ElementName=SystemListScreen}"
                                                    buttonCommandParameter="{Binding ItemsSource.ID, ElementName=SystemList}"/>
                        <!--<Button Content="{Binding ID}"/>-->
                    </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
        </ScrollViewer>
</UserControl>

View 模型:

private List<SystemModel> systems;

public SystemListViewModel()
{
    Systems = new List<SystemModel>();
    Systems = SystemAPI.Instance.GetSystems();
}

public string App { get; set; }

public List<SystemModel> Systems 
{ 
    get { return systems; }
    private set 
    {
        systems = value;
        NotifyChanged("systems");
        NotifyChanged("NoResultsFound");
    } 
}

多行按钮代码:

        public static readonly DependencyProperty buttonCommandProperty = DependencyProperty.Register("buttonCommand", typeof(ICommand), typeof(MultiLineButton));
    public static readonly DependencyProperty buttonCommandParameterProperty = DependencyProperty.Register("buttonCommandParameter", typeof(Object), typeof(MultiLineButton));
    public static readonly DependencyProperty partIDProperty = DependencyProperty.Register("partID", typeof(String), typeof(MultiLineButton));
    public static readonly DependencyProperty companyProperty = DependencyProperty.Register("company", typeof(String), typeof(MultiLineButton));
    public static readonly DependencyProperty typeSorterProperty = DependencyProperty.Register("typeSorter", typeof(String), typeof(MultiLineButton));
    public static readonly DependencyProperty typeLocationProperty = DependencyProperty.Register("typeLocation", typeof(String), typeof(MultiLineButton));

    public MultiLineButton()
    { 
        this.DataContext = this;

        InitializeComponent();
    }

    public String partID
    {
        get { return (String)GetValue(partIDProperty); }
        set { SetValue(partIDProperty, value); }
    }

    public String company
    {
        get { return (String)GetValue(companyProperty); }
        set { SetValue(companyProperty, value); }
    }

    public String typeSorter
    {
        get { return (String)GetValue(typeSorterProperty); }
        set { SetValue(typeSorterProperty, value); }
    }

    public String typeLocation
    {
        get { return (String)GetValue(typeLocationProperty); }
        set { SetValue(typeLocationProperty, value); }
    }

    public ICommand buttonCommand
    {
        get { return (ICommand)GetValue(buttonCommandProperty); }
        set { SetValue(buttonCommandProperty, value); }
    }
    public Object buttonCommandParameter
    {
        get { return (Object)GetValue(buttonCommandParameterProperty); }
        set { SetValue(buttonCommandParameterProperty, value); }
    }

什么不起作用:partID="{Binding ID}", company="{Binding ItemsSource.Company}", typeSorter="{Binding ItemsSource.Name, ElementName=SystemList}", typeLocation="{Binding ItemsSource.Location , ElementName=SystemList}"和 buttonCommandParameter="{Binding ItemsSource.ID, ElementName=SystemList}"。

但是,如果我只使用一个按钮作为带有 Content="{Binding ID}"的数据模板,它可以完美地工作,而且如果我在数据模板之外使用用户控件,它也可以工作。但它不会在数据模板内工作。

我得到的错误是这样的:“BindingExpression 路径错误:在‘object’‘MultiLineButton’(Name=‘’)上找不到‘Company’属性。BindingExpression:Path=Company;DataItem=‘MultiLineButton’(Name =''); 目标元素是 'MultiLineButton' (Name=''); 目标属性是 'company' (type 'String')"

我该如何修复这些绑定(bind)?

最佳答案

  1. 不确定,也许您应该从 DateTemplate 中删除 DataType="SystemModel"
  2. 或者尝试使用简单的textbox(Binding id)作为DataTemplate,看看是否还有空。
  3. 如果以上没有给你任何帮助,试试 Snoop(snoopwpf.codeplex.com) 看看发生了什么。

关于c# - 用户控件作为具有绑定(bind)的数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19929287/

相关文章:

c# - 带密码的 .net 核心中的 Zip 文件

.net - 使用起点 X/Y 和起点 + 扫掠角在 ArcSegment 中获取终点

c# - .Net Core 中不显示 URL 源图像

c# if not null then assign value 的简写

c# - IDataErrorInfo - 它是如何工作的

c# - Linq 生成给定日期范围内缺失记录的列表

c# - C# Windows 应用程序中的 PayPal API

wpf - 使用 MVVM Light Toolkit 在绑定(bind) ViewModel 属性或消息传递之间进行选择以在 ViewModel 和 View 之间进行通信

c# - 快速变化的集合 MVVM WPF - 高 CPU 使用率和 UI 几乎卡住

c# - Command 中 Item 与 UI 的绑定(bind)(初级)