我是一名有用的助手,可以为您翻译文本。
不起作用的部分是: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 路径错误:'Company' 属性在 'object' ''MultiLineButton' (Name='')' 上未找到。BindingExpression:Path=Company;DataItem='MultiLineButton' (Name='');target element 是'MultiLineButton'(Name='');target property 是'company'(type 'String')”。
如何修复这些绑定?
我有一个ItemsControl,其ItemsSource绑定到SystemModels列表。它必须为列表中的每个系统生成一个用户控件。在这些用户控件中,有一些文本框显示系统的名称、ID和位置。
我的代码创建了用户控件,但没有填充用户控件中的文本框。
视图:
<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>
视图模型:
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 路径错误:'Company' 属性在 'object' ''MultiLineButton' (Name='')' 上未找到。BindingExpression:Path=Company;DataItem='MultiLineButton' (Name='');target element 是'MultiLineButton'(Name='');target property 是'company'(type 'String')”。
如何修复这些绑定?