第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在WPF DataGrid中選擇DataGrid Header CheckBox上列的所有

如何在WPF DataGrid中選擇DataGrid Header CheckBox上列的所有

Go
瀟湘沐 2022-01-09 15:43:09
我面臨著 WPF DataGrid Checkboxes C# 的問題。我沒有找到選擇選中標(biāo)題模板復(fù)選框時(shí)選中所有單元格模板的方法。在 viewmodel 中它工作正常。它被全選,但在視圖中它沒有在選中的標(biāo)題復(fù)選框上顯示任何選中的復(fù)選框符號(hào)/標(biāo)記。我遇到的問題與 DataGrid(WPF)中的復(fù)選框有關(guān)單擊此鏈接我想這樣做 我的 XAML 代碼:<DataGrid x:Name="DgLines" ItemsSource="{Binding OpcUaEndpoints}"          MouseDoubleClick="DgLines_MouseDoubleClick" SelectionMode="Extended"            DataContext="{Binding}"  IsReadOnly="True" Grid.ColumnSpan="5">    <DataGrid.Columns>        <DataGridTemplateColumn            <DataGridTemplateColumn.HeaderTemplate>                <DataTemplate>                    <CheckBox Name="ckbSelectedAll"  Checked="ckbSelectedAll_Checked" Unchecked="ckbSelectedAll_Unchecked"                              IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"></CheckBox>                </DataTemplate>            </DataGridTemplateColumn.HeaderTemplate>            <DataGridTemplateColumn.CellTemplate>                <DataTemplate>                    <CheckBox Name="cbkSelect" Checked="cbkSelect_Checked" Unchecked="cbkSelect_Unchecked"                              IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"/>                </DataTemplate>            </DataGridTemplateColumn.CellTemplate>        </DataGridTemplateColumn>        <!--<DataGridTextColumn Width="200" Header="Id" Binding="{Binding Id }" />-->        <DataGridTextColumn Width="200" Header="Name" Binding="{Binding Name}"/>        <DataGridTextColumn Width="500" Header="Description" Binding="{Binding Description}"/>        <DataGridTextColumn Width="500" Header="Lines" Binding="{Binding Endpoint}"/>    </DataGrid.Columns></DataGrid>
查看完整描述

2 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

請(qǐng)找到工作代碼。我對(duì)你的代碼做了一些修改


XAML


    <DataGrid x:Name="DgLines" ItemsSource="{Binding OpcUaEndpoints}" AutoGenerateColumns="False"

                     SelectionMode="Extended"  IsReadOnly="True" Grid.ColumnSpan="5">

                        <DataGrid.Columns>

                            <DataGridTemplateColumn>

                                <DataGridTemplateColumn.HeaderTemplate>

                                <DataTemplate>

                                    <CheckBox Name="ckbSelectedAll" 

                                          IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}">

                                            <i:Interaction.Triggers>

                                                <i:EventTrigger EventName="Checked" >

                                                    <i:InvokeCommandAction Command="{Binding DataContext.CheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />

                                                </i:EventTrigger>

                                                <i:EventTrigger EventName="Unchecked" >

                                                    <i:InvokeCommandAction Command="{Binding DataContext.UncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />

                                                </i:EventTrigger>

                                            </i:Interaction.Triggers>

                                        </CheckBox>

                                </DataTemplate>

                                </DataGridTemplateColumn.HeaderTemplate>

                                <DataGridTemplateColumn.CellTemplate>

                                    <DataTemplate>

                                        <CheckBox Name="cbkSelect" 

                                          IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"/>

                                    </DataTemplate>

                                </DataGridTemplateColumn.CellTemplate>

                            </DataGridTemplateColumn>

            

                            <!--<DataGridTextColumn Width="200" Header="Id" Binding="{Binding Id }" />-->

            

                            <DataGridTextColumn Width="200" Header="Name" Binding="{Binding Name}"/>

                            <DataGridTextColumn Width="500" Header="Description" Binding="{Binding Description}"/>

                            <DataGridTextColumn Width="500" Header="Lines" Binding="{Binding Endpoint}"/>

                        </DataGrid.Columns>

                    </DataGrid>

        

C#


        public class OpcUaEndpointsListViewModel : INotifyPropertyChanged

            {

                private static OpcUaEndpointsListViewModel _instance;

                private static readonly object Padlock = new object();

                private ICommand _addCommand;

                //private ICommand _uncheckCommand;

                private ICommand _searchcommand;

                private ICommand _checkedCommand { get; set; }

                private ICommand _unCheckedCommand { get; set; }

        

                private ObservableCollection<AddOpcUaEndpointsViewModel> _endpoint;

                public string _charNameFromTB;

        

                public event PropertyChangedEventHandler PropertyChanged;

        

                public OpcUaEndpointsListViewModel()

                {

                    BindDataGrid();

                }

        

                public static OpcUaEndpointsListViewModel Instance

                {

                    get

                    {

                        lock (Padlock)

                        {

                            return _instance ?? (_instance = new OpcUaEndpointsListViewModel());

                        }

                    }

                }

        

                /// <summary>

                ///     //OPC UA Endpoint List

                /// </summary>

                public ObservableCollection<AddOpcUaEndpointsViewModel> OpcUaEndpoints

                {

                    get { return _endpoint; }

                    set

                    {

                        if (OpcUaEndpoints == value)

                        {

                            _endpoint = value;

                            OnPropertyChanged("OpcUaEndpoints");

                        }

                    }

                }

        

                public string CharNameFromTB

                {

                    get { return _charNameFromTB; }

                    set

                    {

                        _charNameFromTB = value;

                        OnPropertyChanged("CharNameFromTB");

                    }

                }

        

                public ICommand AddCommand

                {

                    get { return _addCommand ?? (_addCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteAddCommand())); }

                }

        

                public ICommand SearchCommand

                {

                    get { return _searchcommand ?? (_searchcommand = new RelayCommand<object>(p => ExecuteSearchCommand())); }

                }

        

        

                public ICommand CheckedCommand

                {

                    get { return _checkedCommand ?? (_checkedCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteCheckedCommand())); }

                }

                public ICommand UncheckedCommand

                {

                    get { return _unCheckedCommand ?? (_unCheckedCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteUnCheckedCommand())); }

                }

        

        

                private void ExecuteSearchCommand()

                {

                    ///BindDataGridsearch();

                }

        

                private void ExecuteCheckedCommand()

                {

                    foreach (var item in _endpoint)

                    {

                        item.IsSelected = true;

                    }

                }

        

                private void ExecuteUnCheckedCommand()

                {

                    foreach (var item in _endpoint)

                    {

                        item.IsSelected = false;

                    }

                }

        

                private void ExecuteAddCommand()

                {

                    ///BindDataGridsearch();

                }

        

                private void BindDataGrid()

                {

                    _endpoint = new ObservableCollection<AddOpcUaEndpointsViewModel>();

                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "A", Description = "A", Endpoint = 1 });

                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "B", Description = "B", Endpoint = 2 });

                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "C", Description = "C", Endpoint = 3 });

                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "D", Description = "D", Endpoint = 4 });

                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "E", Description = "E", Endpoint = 5 });

                }

        

                public void OnPropertyChanged(string propertyName)

                {

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

                }

            }


public class AddOpcUaEndpointsViewModel : INotifyPropertyChanged

    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)

        {

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        }


        private string _name;


        public string Name

        {

            get { return _name; }

            set { _name = value; OnPropertyChanged("Name"); }

        }


        private string _description;


        public string Description

        {

            get { return _description; }

            set { _description = value; OnPropertyChanged("Description"); }

        }


        private int _endPoint;


        public int Endpoint

        {

            get { return _endPoint; }

            set { _endPoint = value; OnPropertyChanged("Endpoint"); }

        }


        private bool _isSelected;


        public bool IsSelected

        {

            get { return _isSelected; }

            set { _isSelected = value; OnPropertyChanged("IsSelected"); }

        }

    }



查看完整回答
反對(duì) 回復(fù) 2022-01-09
?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊

該DataGrid列定義不繼承DataContext,因?yàn)樗麄儾皇且曈X樹的一部分。


您將不得不使用BindingProxy1 來解決這個(gè)問題。


<DataGrid.Resources>

    <attached:BindingProxy x:Key="proxy" Data="{Binding}"/>

</DataGrid.Resources>

<DataGridTemplateColumn>

    <DataGridTemplateColumn.Header>

        <CheckBox IsChecked="{Binding Data.IsHeaderCheckBoxChecked, Source={StaticResource proxy}}"/>

    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>

        <DataTemplate>

            <CheckBox IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

        </DataTemplate>

    </DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

BindingProxy


public class BindingProxy : Freezable

{

    #region XAML Properties


    public static readonly DependencyProperty DataProperty =

        DependencyProperty.Register(nameof(Data), typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));


    public object Data

    {

        get { return GetValue(DataProperty); }

        set { SetValue(DataProperty, value); }

    }


    #endregion


    #region Freezable


    protected override Freezable CreateInstanceCore()

    {

        return new BindingProxy();

    }


    #endregion

}

另請(qǐng)記住,您的 VM 中沒有 UI 控件,也沒有XXX_Clicked處理程序或類似程序。如有必要,它們屬于代碼隱藏文件 (*.xaml.cs)。


查看完整回答
反對(duì) 回復(fù) 2022-01-09
  • 2 回答
  • 0 關(guān)注
  • 377 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)