【发布时间】:2020-05-13 14:15:47
【问题描述】:
尝试创建一个 Datagrid 并让用户能够通过单击图像来删除一行。生成的窗口示例:
但是,我不知道如何将单击图像与其旁边生成的单元格连接起来,该单元格由文本文件读入。每当我调用该单元格的值时,我可以看到 List,但看不到它的内容。
DataGrid XAML 代码:
<!-- Main Shared Drive Data Grid -->
<DataGrid HorizontalAlignment="Left"
Height="309"
VerticalAlignment="Top"
Width="550"
Margin="24,50,0,0"
Name="SDDataGrid"
Background="Black"
BorderBrush="#26534e"
BorderThickness="4"
Loaded="DataGrid_Loaded"
AutoGenerateColumns="True"
IsReadOnly="True"
RowHeaderWidth="0"
HeadersVisibility="Column"
ColumnWidth="*">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="Foreground" Value="#459289"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="BorderThickness" Value="0,0,2,2"/>
<Setter Property="BorderBrush" Value="#26534e"/>
<Setter Property="Padding" Value="4"/>
<Setter Property="Width" Value="Auto"/>
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="Foreground" Value="#459289"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="BorderThickness" Value="1,1,0,2"/>
<Setter Property="BorderBrush" Value="#26534e"/>
<Setter Property="Padding" Value="4"/>
<Setter Property="Width" Value="Auto"/>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderThickness" Value="0,0,2,0"/>
<Setter Property="BorderBrush" Value="#26534e"/>
<Setter Property="Background" Value="Black"/>
<EventSetter Event="MouseDoubleClick" Handler="Do_Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Width="58">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Delete" Width="57"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="57">
<Button x:Name="deleteBtn" Width="53" Click="deleteBtn_Click">
<Button.Template>
<ControlTemplate>
<Image Source="Assets/trash.png"
Stretch="None"/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#代码:
// Shared Drive List
List<Drives> _list;
// Build The Drive List Object
public class Drives
{
public string Filepath { get; set; }
public Drives(string line)
{
string[] parts = line.Split(',');
this.Filepath = parts[0];
}
public string GetLine()
{
return this.Filepath.ToString();
}
}
// Loads DataGrid Of Window With Drive List
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
var drives = new List<Drives>();
using (StreamReader reader = new StreamReader(@"..\..\Data\SDrives.txt"))
{
while (true)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
drives.Add(new Drives(line));
}
}
this._list = drives;
var grid = SDDataGrid;
grid.ItemsSource = drives;
}
// Adds User Submitted Filepath To Drive List And Reloads Window
private void Add_Btn_Click(object sender, RoutedEventArgs e)
{
var drives = new List<Drives>();
using (StreamWriter writer = new StreamWriter(@"..\..\Data\SDrives.txt", append: true))
{
writer.WriteLine(FilepathTextBox.Text);
}
using (StreamReader reader = new StreamReader(@"..\..\Data\SDrives.txt"))
{
while (true)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
drives.Add(new Drives(line));
}
}
this._list = drives;
var grid = SDDataGrid;
grid.ItemsSource = drives;
}
// Launches Filepath When User Double Clicks
private void Do_Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
var cellInfo = SDDataGrid.CurrentCell;
{
var column = cellInfo.Column as DataGridBoundColumn;
if (column != null)
{
var element = new FrameworkElement() { DataContext = cellInfo.Item };
BindingOperations.SetBinding(element, TagProperty, column.Binding);
var cellValue = element.Tag;
if (Directory.Exists(@"" + cellValue))
{
Process.Start(@"" + cellValue);
}
else
{
System.Windows.MessageBox.Show(@"" + cellValue + " is not a valid filepath.");
}
}
}
}
private void deleteBtn_Click(object sender, RoutedEventArgs e)
{
var selected = SDDataGrid.SelectedItem;
Console.WriteLine(selected.ToString());
}
文本文件:
C:\Users\Edward\Desktop\Projects
C:\Users\Edward\Desktop\School
谢谢!
【问题讨论】: