我有一个ItemsControl,ItemsSource绑定到SystemModel列表。它必须为列表中的每个系统生成一个用户控件。在这些用户控件中,它有一些文本框,显示系统的名称,位置和位置。
我的代码创建了usercontrols,但没有填充usercontrol中的文本框。
查看:
<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}“。
但是,如果我只使用一个按钮作为datatemplate与Content =“{绑定ID}”它工作完美,如果我使用datatemplate外的usercontrol它也工作。但它在datatemplate中不起作用。
我得到的错误是这样的:“BindingExpression路径错误:在'对象'''找不到'公司'属性'''MultiLineButton'(Name ='')'。BindingExpression:Path = Company; DataItem ='MultiLineButton'(名称='');目标元素是'MultiLineButton'(Name ='');目标属性是'company'(类型'String')“
如何修复这些绑定?
答案 0 :(得分:2)
尝试ObservableCollection:
private ObservableCollection<SystemModel> _systems = new ObservableCollection<SystemModel>();
public ObservableCollection<SystemModel> Systems { get { return _systems; } }
public SystemListViewModel()
{
var systems = SystemAPI.Instance.GetSystems();
foreach (var system in systems)
{
Systems.Add(system);
}
}
和xaml应该是:
<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>
<Widgets:MultiLineButton
partID="{Binding ID}"
company="{Binding Company}"
typeSorter="{Binding Name}"
typeLocation="{Binding Location}"
buttonCommand="{Binding DataContext.navigateInspectList,
ElementName=SystemListScreen}"
buttonCommandParameter="{Binding ItemsSource.ID,
ElementName=SystemList}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</UserControl>
正如chao wang所说:
删除DataType="SystemModel"
,因为如果你只使用一种DataType作为DataTemplate,则没有必要。正确的语法是DataType="vm:SystemModel"
,其中vm在父标记中定义,如:xmlns:vm="clr-namespace:MySolution.MyVmProject.MyFolder"
另外,请检查以下内容:
从ItemsSource.
内的Bindings中删除DataTemplate
,因为它只是错误。
仔细检查绑定中的所有名称,因为如果它们错了,在运行时会考虑空值,而你永远不会知道。
检查你的dataContext确保UserControl
将其DataContext放到类型依赖项对象的正确实例中,该对象中包含系统。并确保它保持这种状态。
答案 1 :(得分:2)
DataType="SystemModel"
。