我正在研究一些使用Datagrid(不是DataGridView)的旧C#代码。在不改变所有内容的情况下,我只希望能够从Datagrid的整行中清除数据。
我像这样设置ItemsSource:
this.dg.ItemsSource = Data;
数据定义如下:
public ObservableCollection<DataCollector> Data
{
get { return _Data; }
}
然后代码使用一些像FindChild这样的递归方法来填充它:
TextBlock tb;
Binding b = new Binding("val1");
b.Mode = BindingMode.OneWay;
DGTC1.Header = "A";
DGTC1.Binding = b;
tb = FindChild<TextBlock>(dg, "C1TB");
if (tb != null)
tb.Text = "A";
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
T foundChild = FindChildRecusive<T>(parent, childName);
if (foundChild == null)
{
Log.logItem(LogType.Error, "ViewRunData", MethodBase.GetCurrentMethod().Name,
"Did not find child: " + childName);
}
return foundChild;
}
public static T FindChildRecusive<T>(DependencyObject parent, string childName) where T : DependencyObject
{
if (parent == null)
{
Log.logItem(LogType.Error, "ViewRunData", MethodBase.GetCurrentMethod().Name,
"Did not find parent for child: " + childName);
return null;
}
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;
if (childType == null)
{
foundChild = FindChildRecusive<T>(child, childName);
if (foundChild != null) break;
}
else
{
if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
else
{
foundChild = FindChildRecusive<T>(child, childName);
if (foundChild != null)
{
break;
}
}
}
else
{
foundChild = (T)child;
break;
}
}
}
return foundChild;
}
这是xaml文件的snippit
<Custom:DataGrid x:Name="dg" Visibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Height="176"
CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"
Width="785" Canvas.Top="18" AutoGenerateColumns="False"
ItemsSource="{Binding Data}" >
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn x:Name="sampleIdColumn" Header="Sample ID" CanUserReorder="False" CanUserSort="False" Width="360"
Binding="{Binding Suspect_ID, Mode=OneWay}" FontSize="12">
<Custom:DataGridTextColumn.HeaderTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="Sample ID" HorizontalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="270"/>
</TextBlock.LayoutTransform>
</TextBlock>
</HierarchicalDataTemplate>
</Custom:DataGridTextColumn.HeaderTemplate>
</Custom:DataGridTextColumn>
<Custom:DataGridTextColumn x:Name="DGTC1" Width="35"
Binding="{Binding D3S1358Val1, Mode=OneWay}" CanUserReorder="False" CanUserSort="False">
<Custom:DataGridTextColumn.HeaderTemplate>
<HierarchicalDataTemplate>
<TextBlock x:Name="C1TB" Text="">
<TextBlock.LayoutTransform>
<RotateTransform Angle="270"/>
</TextBlock.LayoutTransform>
</TextBlock>
</HierarchicalDataTemplate>
</Custom:DataGridTextColumn.HeaderTemplate>
</Custom:DataGridTextColumn>
<Custom:DataGridTextColumn x:Name="DGTC2" Width="35"
Binding="{Binding D3S1358Val2, Mode=OneWay}" CanUserReorder="False" CanUserSort="False">
<Custom:DataGridTextColumn.HeaderTemplate>
<HierarchicalDataTemplate>
<TextBlock Name="C2TB" Text="">
<TextBlock.LayoutTransform>
<RotateTransform Angle="270"/>
</TextBlock.LayoutTransform>
</TextBlock>
</HierarchicalDataTemplate>
</Custom:DataGridTextColumn.HeaderTemplate>
</Custom:DataGridTextColumn>
如何在设置ItemsSource后清除行中的数据?我查看了CurrentItem方法,但无法弄清楚如何实现它。
答案 0 :(得分:1)
您可以通过从Data
移除相应的ObservableCollection<Data>
项来清除行:
Data.RemoveAt(0); // 0 is the index of the first row
...或者通过将相应Data
对象的所有属性设置为一些空白值:
var row = Data[0];
row.D3S1358Val1 = "";
//...
要使后者发挥作用,Data
类必须实现INotifyPropertyChanged
界面并提出更改通知:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx