我现在遇到问题,我需要根据 1 单元格值删除datagrid
行,代码示例如下:
XAML:
<DataGrid
x:Name="AccountGrid"
IsSynchronizedWithCurrentItem="True"
GridLinesVisibility="Vertical"
ItemsSource="{Binding}"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
AutoGenerateColumns="False"
CanUserResizeRows="False"
HorizontalContentAlignment="Left"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Margin="208,0,0,135"
>
<DataGrid.Columns>
<DataGridTextColumn Header="VM-Name" Binding="{Binding Path=vmName}" MinWidth="80" IsReadOnly="True" />
<DataGridTextColumn Header="Profile" Binding="{Binding Path=profileN}" MinWidth="100" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
我如何添加项目:
public class AddToGrid
{
public string vmName { get; set; }
public string profileN { get; set; }
}
this.Dispatcher.Invoke(() =>
{
var dat = new AddToGrid { vmName = clientSockets.Find(item => item.Item1 == current).Item2, profileN = profile.Item1 };
AccountGrid.Items.Add( dat );
});
我需要做的是使用grid
从VM-Name
删除一行,提前致谢!
答案 0 :(得分:0)
AddToGrid
课程重命名为MyGridRow
,并将其移至公共类型而非嵌套类型。using System.Linq;
和using System.Collections.ObjectModel;
添加到您的C#代码隐藏文件中。在您的XAML代码隐藏中,添加以下公共属性:
public ObservableCollection<MyGridRow> MyGridRows { get; } = new ObservableCollection<MyGridRow>();
将您的XAML更改为:
<DataGrid ItemsSource="{Binding MyGridRows}" ...>
</DataGrid>
更改添加项目的代码:
String vmName = clientSockets.Find( item => item.Item1 == current ).Item2;
String profile = profileN = profile.Item1;
this.MyGridRows.Add( new MyGridRow { VMName = vmName, ProfileN = profile } );
要删除行,请执行以下操作:
MyGridRow itemToRemove = this.MyGridRows
.FirstOrDefault( pair => pair.Item.VMName == "someName" )
if( itemToRemove != null )
{
this.MyGridRows.Remove( itemToRemove );
}