当前位置:网站首页>[WPF binding 3] listview basic binding and data template binding

[WPF binding 3] listview basic binding and data template binding

2022-04-23 17:08:00 code bean

【WPF binding 1】 ListBox Basic binding

It introduces ListBox Basic binding of , Record today ListView Related binding of .

if ListBox It's a list , that ListView It's a multi list with column headings .

Such as :

This list has a feature , The first and second columns are strings , The third column is a control TextBox.

So today we will use two kinds of bindings , Basic binding and template binding (DataTemplate)

Basic binding and ListBox similar ,ListBox Yes DisplayMemberPath attribute

<ListBox Name="list_axis" DisplayMemberPath="Name" SelectedValuePath="Num" SelectionChanged="list_axis_SelectionChanged"/>

ListView There is an attribute called DisplayMemberBinding, Because there is more than one column , So we need to make a dolly , like this :

<ListView x:Name="lv_pos" ItemContainerStyle="{StaticResource ListViewItemStyle}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header=" name " Width="auto" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header=" Alias " Width="auto" DisplayMemberBinding="{Binding ShowName}"/>           
        </GridView>
    </ListView.View>
</ListView>

The third column , I use it Textbox Control , This supports editing , Therefore, template binding is required , like this :

<ListView x:Name="lv_pos" ItemContainerStyle="{StaticResource ListViewItemStyle}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header=" name " Width="auto" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header=" Alias " Width="auto" DisplayMemberBinding="{Binding ShowName}"/>
                    <GridViewColumn Header=" Location " Width="120">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Width="auto" MinWidth="80" Text="{Binding Path=Pos, Mode=TwoWay}"   />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

Finally, let's look at the data source section :

First customize a class :

    public class AxisPosInfo
    {
        public AxisPosInfo(string name, string show_name, double pos)
        {
            Name = name;
            ShowName = show_name;
            Pos = pos;
        }

        public AxisPosInfo()
        {
        }

        public string Name { get; set; }

        public string ShowName { get; set; }

        public double Pos { get; set; }

    }

Define an array in the :

public List<AxisPosInfo> PosList { get; set; }

The use of ListView Of ItemsSource Bind with data source .

lv_pos.ItemsSource = axis_info.PosList;

版权声明
本文为[code bean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231702084890.html