简体   繁体   中英

Data not displaying in styled WPF ListView

I'm trying to create an application at work to display jobs assigned to our team members and this is the first time I've worked with WPF. I did a simple test project before this to make sure I understood how things work in WPF compared to WindowForms and I am stuck on getting my data to display in my listviews. During the test project I was able to set and display the data fine, but once I moved to the live project and started making styling and really segregating the code for readability and maintainability my issue arises. Here is the XAML code for the list view style:

 <Style x:Key="baseListViewstyle" TargetType="ListView">
                <Setter Property="FontFamily" Value="Gautami"/>
                <Setter Property="FontSize" Value="10"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListView">
                            <ListView>
                                <ListView.View>
                                    <GridView>
                                        <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="PRF"       DisplayMemberBinding="{Binding Path=[0]}"/>
                                        <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Client ID" DisplayMemberBinding="{Binding Path=[1]}"/>
                                        <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Drop Date" DisplayMemberBinding="{Binding Path=[2]}"/>
                                        <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="QTY"       DisplayMemberBinding="{Binding Path=[3]}"/>
                                        <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Type"      DisplayMemberBinding="{Binding Path=[4]}"/>
                                    </GridView>
                                </ListView.View>
                            </ListView>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

I'm 100% certain the issue is from this style, but I'm not sure how to resolve it. I've taken out the style and the ListViews will display all the items being added into them from the code behind, but there are no headers (obviously) and it merely displays the type of the item added in (in this case the ListViews state String[] x number of times). I would rather not move the header declarations into the window's XAML but keep it segregated in my style.xaml, as these ListViews are going to be reused a dozen times or so throughout the code, and most of which will be added in dynamically, so pointing to unified style will make maintenance exponentially easier. I feel like this is a simple issue in the style, maybe I need a different binding or should the headers be declared differently since it is happening in a style? For reference here is the code behind where I am adding the data to the list view.

private static void populateListView(ListView lv, List<Label> labels)
    {
        if (lv.Tag != null)
        {
            SqlConnectionStringBuilder conString = new SqlConnectionStringBuilder();
            conString.IntegratedSecurity = true;
            conString.DataSource = "oh50ms04\\Server_1";

            using (SqlConnection con = new SqlConnection(conString.ToString()))
            {
                con.Open();
                string sqlCMD = getSQLCommand(lv.Tag.ToString(), labels);
                SqlCommand cmd = new SqlCommand(sqlCMD, con);
                try
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {                                
                            string[] arr = new string[] { reader["JobID"].ToString(), reader["ClientID"].ToString(), DateTime.Parse(reader["RDD"].ToString()).ToString("MM/dd/yy"), reader["QuantityFinishedPieces"].ToString(), reader["Type"].ToString() };
                            lv.Items.Add(arr);
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }

The ControlTemplate defines the (entire) appearance of a control so you are effectively "overwriting" the appearance of the ListView that you are populating with the empty one in your Style.

Try to set the View property of the ListView to a GridView in the Style instead of overriding the template:

<Style x:Key="baseListViewstyle" TargetType="ListView" x:Shared="False">
    <Setter Property="FontFamily" Value="Gautami"/>
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="View">
        <Setter.Value>
            <GridView>
                <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="PRF"       DisplayMemberBinding="{Binding Path=[0]}"/>
                <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Client ID" DisplayMemberBinding="{Binding Path=[1]}"/>
                <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Drop Date" DisplayMemberBinding="{Binding Path=[2]}"/>
                <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="QTY"       DisplayMemberBinding="{Binding Path=[3]}"/>
                <GridViewColumn Width="{StaticResource ResourceKey=columnWidth}" Header="Type"      DisplayMemberBinding="{Binding Path=[4]}"/>
            </GridView>
        </Setter.Value>
    </Setter>
</Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM