欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf自定义绑定数据

程序员文章站 2022-01-05 10:50:43
...

 自定义绑定数据,通过Binding Path进行绑定

1、ItemsSource="{Binding SelFloorData}"(在datagrid中绑定数据)

2、Binding="{Binding FloorName}(绑定具体的数据成员)

3、解决checkbox中需要点击两次才能选中,重写DataGridTemplateColumn

4、列头和内容居中

<DataGrid x:Name="dataGrid" HorizontalAlignment="Center" VerticalAlignment="Top" Height="330" Width="170" Margin="8,0,0,0" 
                  RowHeaderWidth="0" HorizontalContentAlignment="Center" IsSynchronizedWithCurrentItem="True" 
                  ItemsSource="{Binding SelFloorData}" CanUserAddRows="False" AutoGenerateColumns="False"
                  >
            
            <!--为了解决CheckBox选中两次才可改变状态-->
            <DataGrid.Columns>
                <!--相关属性不能放到CheckBox那一行,否则影响居中等-->
                <DataGridTemplateColumn Header="选择" CanUserResize="False" Width="40" >
                    <!--列头居中-->
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <!--内容居中-->
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <!--绑定数据-->
                            <CheckBox Name="SelCheckBox" HorizontalAlignment="Center" IsChecked="{Binding SelCurFloor}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="楼层" Width="60">
                    <!--列头居中-->
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <!--内容居中-->
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock HorizontalAlignment="Center" Text="{Binding FloorName}"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="标高" Width="*">
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock HorizontalAlignment="Center" Text="{Binding FloorLevel}"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
public class SelFloorData : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 是否选择
        /// </summary>
        private bool m_bSelCurFloor;
        public bool SelCurFloor
        {
            get { return m_bSelCurFloor; }
            set { m_bSelCurFloor = value; if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SelCurFloor")); } }
        }


        /// <summary>
        /// 楼层名称
        /// </summary>
        private string m_strFloorName;
        public string FloorName
        {
            get { return m_strFloorName; }
            set { m_strFloorName = value; if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("FloorName")); } }
        }


        /// <summary>
        /// 楼层标高
        /// </summary>
        public string m_strFloorLevel;
        public string FloorLevel
        {
            get { return m_strFloorLevel; }
            set { m_strFloorLevel = value; if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("FloorLevel")); } }

        }

    }

 

相关标签: C# wpf