以编程方式在加载时替换 GridView 单元格内容
我第一次在 WPF 中编写程序。我有一个 GridView 模式下的 ListView,显示来自绑定数据集的数据(从数据库中获取)。
在我的数据库中,“出生日期”不是必填字段。因此,任何没有 dob 的记录的值都设置为 DateTime.MinValue
。在每个最小值日期,日期在单元格中显示为 01/01/0001。我正在尝试找到一种方法来格式化单元格,以便 DateTime.MinValue
不显示,或者将每个 MinValue
替换为 ""
。
我的想法是使用日期所在文本块的“Loaded”事件并替换“01/01/0001”的每个实例,或者在将数据集发送到 GridView 之前循环遍历数据集并在那里删除/替换它们。我也没有运气弄清楚该怎么做。
我的 GridView 的 xaml 代码是:
<Grid>
<ListView x:Name="resultsListView" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" Margin="0,54,0,28" ItemsSource="{Binding Path=Table}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"
Header="Last Name"
Width="150"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}"
Header="First Name"
Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=MiddleName}"
Header="Middle Name"
Width="100"/>
<GridViewColumn Header="Date of Birth" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Justify" Text="{Binding Path=DateOfBirth, StringFormat='{}{0:MM/dd/yyyy}'}" Loaded="TextBlock_Loaded" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
数据集代码:
private void FillListView(DataSet ds)
{
if (resultsListView.Items.Count != 0)
{
resultsListView.Items.Clear();
}
resultsListView.DataContext = ds.Tables[0].DefaultView;
}
任何有关如何在我的 GridView 中显示 DateTime.MinValue
空白的建议将不胜感激!
I am working on a program in WPF for the first time. I have a ListView in GridView mode displaying data from a bound dataset (which is grabbed from a database).
In my database, "date of birth" is not a required field. As such, any record without a dob had the value set to DateTime.MinValue
. On each of these minimum value dates, the date shows in the cell as 01/01/0001. I am trying to find a way to either format the cell so that DateTime.MinValue
doesn't show, or replace each MinValue
with ""
.
My thought was to either use the "Loaded" event of the textblock the date is in and replace each instance of "01/01/0001", or loop through the dataset before sending it to the GridView and remove/replace them there. I've not had any luck figuring out how to do either.
My xaml code for the GridView is:
<Grid>
<ListView x:Name="resultsListView" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" Margin="0,54,0,28" ItemsSource="{Binding Path=Table}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"
Header="Last Name"
Width="150"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}"
Header="First Name"
Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=MiddleName}"
Header="Middle Name"
Width="100"/>
<GridViewColumn Header="Date of Birth" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Justify" Text="{Binding Path=DateOfBirth, StringFormat='{}{0:MM/dd/yyyy}'}" Loaded="TextBlock_Loaded" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
Code for the DataSet:
private void FillListView(DataSet ds)
{
if (resultsListView.Items.Count != 0)
{
resultsListView.Items.Clear();
}
resultsListView.DataContext = ds.Tables[0].DefaultView;
}
Any advice on how to show blanks for DateTime.MinValue
in my GridView would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建一个 IValueConverter 来处理此问题,并将其包含在您的绑定表达式中。
在您的资源中:
然后更新您的绑定:
然后定义类:
这有两个方法。您只需实现
Convert
(除非您计划使用双向绑定)。在此方法中,您可以通过参数获取格式字符串(正如我在上面传递的绑定表达式一样),还可以检查DateTime.MinValue
并返回一个空白字符串。I would make an
IValueConverter
that deals with this, and include it in your binding expression.In your resources:
Then update your binding:
Then define the class:
This has two methods. You need only to implement
Convert
(unless you're planning on using two-way binding). In this method, you can take the format string via a paramater (as I've passed in the binding expression above) and also check forDateTime.MinValue
and return a blank string.