카테고리 없음

[씨#] xaml에서 DataGrid의 강조 표시된 셀 설정

행복을전해요 2021. 1. 21. 02:09

문제는 선택한 행 / 셀이 초점이 여전히 listView 항목에 있기 때문에 초점이 맞지 않는다는 것입니다. xaml에서 DataGridCell 요소의 스타일을 지정할 수 있습니다. 다음은이를 보여주는 작은 코드입니다.

<Window.Resources>
    <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Background" Value="Red"/>
                                                    </Trigger>
                                                            </Style.Triggers>
                                                                </Style>
                                                                </Window.Resources>
                                                                <Grid>
                                                                    <Grid.ColumnDefinitions>
                                                                            <ColumnDefinition Width="*"/>
                                                                                    <ColumnDefinition Width="Auto"/>
                                                                                        </Grid.ColumnDefinitions>
                                                                                            <DataGrid ItemsSource="{Binding Tests}" 
                                                                                                          SelectedItem="{Binding GridSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                                                                        SelectedIndex="{Binding SelectedGridIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                                                                                                        
                                                                                                                        </DataGrid>
                                                                                                                            <Button Command="{Binding ChangeSelectedItemCommand}" 
                                                                                                                                        Content="Change Grid Selected item" 
                                                                                                                                                    Grid.Column="1" 
                                                                                                                                                                VerticalAlignment="Top"/>
                                                                                                                                                                </Grid>
                                                                                                                                                                

그리고 여기 viewModel 부분 :

public class MainWindowViewModel : INotifyPropertyChanged
{
    #region Private members
    
        private List<TestClass> _tests;
            private TestClass _gridSelectedItem;
                private ICommand _changeSelectedItemCommand;
                    private int _selectedGridIndex;
                        #endregion
                        
                            #region Constructor
                            
                                public MainWindowViewModel()
                                    {
                                            Tests = new List<TestClass>();
                                                    for (int i = 0; i < 25; i++)
                                                            {
                                                                        TestClass testClass= new TestClass {Name = "Name " + i, Title = "Title" + i};
                                                                                    Tests.Add(testClass);
                                                                                            }
                                                                                                }
                                                                                                
                                                                                                    #endregion
                                                                                                    
                                                                                                        #region Public properties
                                                                                                            public List<TestClass> Tests
                                                                                                                {
                                                                                                                        get { return _tests; }
                                                                                                                                set
                                                                                                                                        {
                                                                                                                                                    _tests = value;
                                                                                                                                                                OnPropertyChanged("Tests");
                                                                                                                                                                        }
                                                                                                                                                                            }
                                                                                                                                                                            
                                                                                                                                                                                public TestClass GridSelectedItem
                                                                                                                                                                                    {
                                                                                                                                                                                            get { return _gridSelectedItem; }
                                                                                                                                                                                                    set
                                                                                                                                                                                                            {
                                                                                                                                                                                                                        _gridSelectedItem = value;
                                                                                                                                                                                                                                    OnPropertyChanged("GridSelectedItem");
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                    public int SelectedGridIndex
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                get { return _selectedGridIndex; }
                                                                                                                                                                                                                                                                        set
                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                            _selectedGridIndex = value;
                                                                                                                                                                                                                                                                                                        OnPropertyChanged("SelectedGridIndex");
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                        #endregion
                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                            public ICommand ChangeSelectedItemCommand
                                                                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                                                                        get { return _changeSelectedItemCommand ?? (_changeSelectedItemCommand = new  SimpleCommand(p => ChangeSelectedGridItem())); }
                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                private void ChangeSelectedGridItem()
                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                            SelectedGridIndex++;
                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                    #region INotifyPropertyChanged
                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                        public event PropertyChangedEventHandler PropertyChanged;
                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                            [NotifyPropertyChangedInvocator]
                                                                                                                                                                                                                                                                                                                                                                                protected virtual void OnPropertyChanged(string propertyName)
                                                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                                                            PropertyChangedEventHandler handler = PropertyChanged;
                                                                                                                                                                                                                                                                                                                                                                                                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                            #endregion
                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                            

데모 객체 클래스 :

public class TestClass
{
    public string Title { get; set; }
        public string Name { get; set; }
        }
        

및 일부 명령 클래스 :

public class SimpleCommand : ICommand
{

    private readonly Predicate<object> _canExecuteDelegate;
    
        private readonly Action<object> _executeDelegate;
        
            #region Constructors
            
                public SimpleCommand(Action<object> execute)
                        : this(execute, null)
                            {
                                }
                                
                                    public SimpleCommand(Action<object> execute, Predicate<object> canExecute)
                                        {
                                                if (execute == null)
                                                        {
                                                                    throw new ArgumentNullException("execute");
                                                                            }
                                                                            
                                                                                    _executeDelegate = execute;
                                                                                            _canExecuteDelegate = canExecute;
                                                                                                }
                                                                                                
                                                                                                    #endregion // Constructors
                                                                                                    
                                                                                                        #region ICommand Members
                                                                                                        
                                                                                                            public virtual bool CanExecute(object parameter)
                                                                                                                {
                                                                                                                        return _canExecuteDelegate == null || _canExecuteDelegate(parameter);
                                                                                                                            }
                                                                                                                            
                                                                                                                                public event EventHandler CanExecuteChanged
                                                                                                                                    {
                                                                                                                                            add { CommandManager.RequerySuggested += value; }
                                                                                                                                                    remove { CommandManager.RequerySuggested -= value; }
                                                                                                                                                        }
                                                                                                                                                        
                                                                                                                                                            public void Execute(object parameter)
                                                                                                                                                                {
                                                                                                                                                                        _executeDelegate(parameter);
                                                                                                                                                                            }
                                                                                                                                                                            
                                                                                                                                                                                #endregion
                                                                                                                                                                                }
                                                                                                                                                                                

ViewModel에 대해 알 수 있도록 View의 DataContext를 추가해야합니다.

 public MainWindow()
    {
            InitializeComponent();
                    DataContext = new MainWindowViewModel();
                        }
                        

원하는 효과를 얻는 데 도움이 되었기를 바랍니다.



출처
https://stackoverflow.com/questions/22008884