简体   繁体   English

WPF DataGrid用户控件 - 在视图中添加新列

[英]WPF DataGrid User Control - adding a new column in a view

I have a user control that has a Datagrid that has 3 columns. 我有一个用户控件,其Datagrid有3列。 I want to use this user control in a view but I need to add one more column to the Datagrid but only in this view. 我想在视图中使用此用户控件,但我需要向Datagrid添加一个列,但在此视图中。

Is this possible? 这可能吗?

Code on the view 视图上的代码

// Initialising the Usercontrol on the new view
xmlns:myuct="clr-namespace:Customer.UserControls">
<Grid>
    <myuct:CustomerSearch x:Name="CS"/>
</Grid>

This shows the Datagrid and other items of the user control in the view correctly. 这会在视图中正确显示Datagrid和用户控件的其他项。

Any help would be grateful. 任何帮助将不胜感激。

I will suggest that you make your usercontrol accept a flag or switch that can show and hide the additional column. 我建议您让usercontrol接受可以显示和隐藏其他列的标志或开关。 Use DependencyProperties so that you can set the flag/switch in XAML. 使用DependencyProperties,以便您可以在XAML中设置标志/开关。

// Initialising the Usercontrol on the new view
xmlns:myuct="clr-namespace:Customer.UserControls">
<Grid>
    <myuct:CustomerSearch x:Name="CS" ShowAddOnColumn="true"/>
</Grid>

To programatically add a column: 以编程方式添加列:

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "First Name"; 
textColumn.Binding = new Binding("FirstName"); 
dataGrid.Columns.Add(textColumn); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM