我一直在寻找这个,但我没有找到一些正在做我想要的东西。下面附带的截图是我想要创建的,这是在VBA中完成的,但我尝试使用WPF在C#中创建它。
我正在导入具有以下结构的CSV文件:
的 “ID”, “NAME”,CheckedINT
例如
“1662”,“BØRSTER”,1 < - 1 =已选中,0 =未选中。
因此,当我导入文件时,它会在列表视图中显示信息。但是,我无法为每个项目/记录创建该复选框。我试图在XAML文件中创建它 如果你需要一些代码,例如要将数据加载到列表视图中,请告诉我。 我对C#很新,所以非常感谢(使用VS15)。
XAML:
<Window x:Class="CloneMacro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CloneMacro"
mc:Ignorable="d"
Title="Clone Macro" Height="350" Width="592.045" Closed="MainWindow_Closed" Icon="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\copy.png">
<Grid Background="#FFDEDEDE" Margin="-1,0,1,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="71*"/>
<ColumnDefinition Width="399*"/>
<ColumnDefinition Width="114*"/>
</Grid.ColumnDefinitions>
<Button x:Name="cmdNew" HorizontalAlignment="Left" Margin="39,36,0,0" VerticalAlignment="Top" Width="39" Height="40" ToolTip="New" Grid.ColumnSpan="2" Click="cmdNew_Click">
<Image x:Name="imgNew" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="36" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\Generic_Document.png" RenderTransformOrigin="0.172,1.089"/>
</Button>
<Button x:Name="cmdOpen" HorizontalAlignment="Left" Margin="8,36,0,0" VerticalAlignment="Top" Width="39" Height="40" ToolTip="Open" Grid.Column="1" Click="cmdOpen_Click">
<Image x:Name="imgOpen" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="36" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\folder_Open_32xLG.png"/>
</Button>
<Button x:Name="cmdSave" HorizontalAlignment="Left" Margin="48,36,0,0" VerticalAlignment="Top" Width="39" Height="40" ToolTip="Save" Grid.Column="1" Click="cmdSave_Click">
<Image x:Name="imgSave" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="36" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\save_16xLG.png"/>
</Button>
<Button x:Name="cmdDelete" HorizontalAlignment="Left" Margin="139,36,0,0" VerticalAlignment="Top" Width="39" Height="40" Grid.Column="1" Click="cmdDelete_Click">
<Image x:Name="imgDelete" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="32" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\action_Cancel_16xLG.png" ToolTip="Delete" RenderTransformOrigin="1.334,0.534"/>
</Button>
<Button x:Name="cmdNewStore" HorizontalAlignment="Left" Margin="179,36,0,0" VerticalAlignment="Top" Width="39" Height="40" Grid.Column="1" Click="cmdNewStore_Click">
<Image x:Name="imgNewStore" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="36" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\action_add_16xLG.png" ToolTip="Add New Store" RenderTransformOrigin="0.5,0.5"/>
</Button>
<Button x:Name="cmdExecute" HorizontalAlignment="Left" Margin="259,36,0,0" VerticalAlignment="Top" Width="39" Height="40" ToolTip="Execute" Grid.Column="1" Click="cmdExecute_Click">
<Image x:Name="image" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="37" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\StatusAnnotations_Play_32xLG_color.png"/>
</Button>
<Button x:Name="cmdCancel" HorizontalAlignment="Left" Margin="299,36,0,0" VerticalAlignment="Top" Width="39" Height="40" ToolTip="Cancel" Grid.Column="1" Click="cmdCancel_Click">
<Image x:Name="imgCancel" HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="36" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\CancelApproval_32x32.png"/>
</Button>
<CheckBox x:Name="chOpenLastUsed" Content="Open Last Used File" Margin="360,48,0,0" VerticalAlignment="Top" Height="21" Width="147" HorizontalAlignment="Left" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListView x:Name="lvStores" HorizontalAlignment="Left" Height="161" Margin="39,98,0,0" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="ID" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding sID}" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="Auto" DisplayMemberBinding="{Binding Path=sName}"/>
<GridViewColumn Header="Import File" Width="Auto" DisplayMemberBinding="{Binding Path=sImportFile}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<Label x:Name="lblProgress" x:FieldModifier="public" Content="Status Text" HorizontalAlignment="Left" Margin="40,264,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="369" BorderThickness="1" BorderBrush="#FF7C7C7C"/>
<Button x:Name="ComDialog" Grid.Column="1" HorizontalAlignment="Left" Margin="359,119,0,0" VerticalAlignment="Top" Width="48" Height="51" Grid.ColumnSpan="2">
<Image x:Name="imgComDialog" HorizontalAlignment="Left" Height="44" VerticalAlignment="Top" Width="48" Source="C:\Users\Niclas VMWare\Documents\Visual Studio 2015\Projects\CloneMacro\CloneMacro\images\DialogID_6220_32x.png"/>
</Button>
</Grid>
</Window>
.CS
public MainWindow()
{
// Leave this to show the UI
InitializeComponent();
lblProgress.Content = "";
lvStores.ItemsSource = ReadCSV(@"C:\Users\Niclas VMWare\Downloads\TEST FILE.pcf");
//SetButtonState();
string sFile;
}
public class Store
{
public string sName { get; set; }
public string sImportFile { get; set; }
public string sID { get; set; }
public bool IsChecked { get; set; }
public Store(string id, string strName, string isChecked, string strImportFile)
{
sName = strName.Replace("\"","");
sImportFile = strImportFile;
sID = id.Replace("\"","");
int iBool = Convert.ToInt32(isChecked);
switch (iBool)
{
case 0: IsChecked = false; break;
case 1: IsChecked = true; break;
default: throw new InvalidOperationException("Third value in PCF file must be 0 or 1!");
}
}
}
public IEnumerable<Store> ReadCSV(string fileName)
{
// Check file extension
string[] lines = File.ReadAllLines(Path.ChangeExtension(fileName, ".pcf"), Encoding.GetEncoding(65001));
// lines.Select allows to project each line as a Store.
// This will give IEnumerable<Store> back.
return lines.Select(line =>
{
string[] data = line.Split(',');
// Return store data with the data in order.
return new Store(data[0], data[1], data[2], fileName);
});
}
答案 0 :(得分:1)
将listview的第一列定义修改为
<GridViewColumn Header="ID" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding sID}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我相信它会解决你的问题。数据绑定可能会受到影响,您需要更多的XAML /代码来处理它。
我在这里做的是采用GridViewColumn数据模板的默认定义,并将其替换为我自己的。您可以在Data templating主题中详细了解它。
答案 1 :(得分:1)
教我错了,但您的csv文件包含ID, Name, IsChecked
列,您将其解析为new Store(id: data[0], strName: data[1], strImportFile: data[2])
。
因此,按照您的示例,新Store
对象的属性将具有以下值:
public class Store
{
public string sName { get; set; } // BØRSTER
public string sImportFile { get; set; } // 1
public string sID { get; set; } // 1662
public Store(string id, string strName, string strImportFile)
{
sName = strName;
sImportFile = strImportFile;
sID = id;
}
}
您应该为您的类添加另一个属性,并为构造函数添加另一个参数:
public bool IsChecked { get; set; }
public Store(string id, string strName, string isChecked, string strImportFile)
{
sID = id;
sName = strName;
IsChecked = isChecked == "1";
sImportFile = strImportFile;
}
现在您可以通过调用
来创建新的Store实例
new Store(data[0], data[1], data[2], fileName)
。
绑定现在应该看起来像
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding sID}" />
至少让我给你一个善意的建议。您可以查看Microsoft Naming Guidelines。 例如:属性应该在PascalCasing中命名,你也应该避免使用像strName这样的匈牙利符号。但这只是我个人的意见。