标签:user-controls datatemplate wpf c mvvm
我有一个ItemsControl,并将ItemsSource绑定到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>
ViewModel:
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}”的数据模板,则效果很好,如果我在数据模板之外使用usercontrol,它也可以工作.但是它在数据模板内部不起作用.
我得到的错误是这样的:“ BindingExpression路径错误:在’object’“MultiLineButton'(Name =”)’上找不到’Company’属性.BindingExpression:Path = Company; DataItem =’MultiLineButton'(Name =” );目标元素是’MultiLineButton'(Name =”);目标属性是’company'(类型’String’)“
我该如何解决这些绑定问题?
解决方法:
>不确定,也许您应该从DateTemplate中删除DataType =“ SystemModel”.
>或尝试使用简单的文本框(绑定ID)作为DataTemplate,看看是否仍然为空.
>如果上述方法没有帮助您,请尝试Snoop(snoopwpf.codeplex.com)看看发生了什么.
标签:user-controls,datatemplate,wpf,c,mvvm 来源: https://codeday.me/bug/20191030/1966145.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。