以编程方式在加载时替换 GridView 单元格内容

发布于 2024-08-07 08:35:39 字数 1918 浏览 6 评论 0原文

我第一次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-08-14 08:35:39

我将创建一个 IValueConverter 来处理此问题,并将其包含在您的绑定表达式中。

在您的资源中:

<local:DateTimeConverter x:Key="DateTimeConverter" />

然后更新您的绑定:

<TextBlock Text="{Binding Path=DateOfBirth, 
                          Converter={StaticResource DateTimeConverter},
                          ConverterParameter='MM/dd/yyyy'}" />

然后定义类:

public class DateTimeConverter : IValueConverter

这有两个方法。您只需实现 Convert(除非您计划使用双向绑定)。在此方法中,您可以通过参数获取格式字符串(正如我在上面传递的绑定表达式一样),还可以检查 DateTime.MinValue 并返回一个空白字符串。

I would make an IValueConverter that deals with this, and include it in your binding expression.

In your resources:

<local:DateTimeConverter x:Key="DateTimeConverter" />

Then update your binding:

<TextBlock Text="{Binding Path=DateOfBirth, 
                          Converter={StaticResource DateTimeConverter},
                          ConverterParameter='MM/dd/yyyy'}" />

Then define the class:

public class DateTimeConverter : IValueConverter

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 for DateTime.MinValue and return a blank string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文