久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C# WPF DataGrid獲取單元格并改變背景色

 ontheroad96j47 2022-01-08

01

概述

WPF 自帶了一個(gè)表格控件datagrid,,這個(gè)控件類似winfrom中的datagridview,在數(shù)據(jù)顯示的時(shí)候也經(jīng)常會(huì)用到,,這節(jié)主要講解如何從后臺代碼獲取到單元格控件并改變其相關(guān)屬性:背景色、前景色,、字體等,。

02


效果演示

03


代碼

后臺cs:

using System;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Controls.Primitives;using System.Windows.Media;
namespace Caliburn.Micro.Hello{ /// <summary> /// ShellView.xaml 的交互邏輯 /// </summary> public partial class ShellView : UserControl { public ShellView() { InitializeComponent(); }
private void DG_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { int colindex = -1; int rowindex = -1;
//方法1 //DataGridCellInfo info = new DataGridCellInfo(dg.Items[0], dg.Columns[2]); //方法2 //foreach (DataGridCellInfo info in this.dgSourceData.SelectedCells) //{ // string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text; // Console.WriteLine(str); //} //方案1 var info = this.dgSourceData.SelectedCells.FirstOrDefault(); var str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text; //((TextBlock)info.Column.GetCellContent(info.Item)).Foreground = new SolidColorBrush(Colors.Red);            //Console.WriteLine(str);
//方案2 colindex = this.dgSourceData.CurrentCell.Column.DisplayIndex;//獲取選中單元格列號 //rowindex = this.dgSourceData.SelectedIndex;//獲取選中單元格行號 for (int i = 0; i < ShellViewModel.StudentList.Count(); i++) { if (ShellViewModel.StudentList[i] == this.dgSourceData.CurrentItem) { //MessageBox.Show("當(dāng)前選擇的行是:" + i.ToString()); rowindex = i; } } DataGridRow row = (DataGridRow)dgSourceData.ItemContainerGenerator.ContainerFromIndex(rowindex);//獲取選中單元格所在行 DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);//函數(shù)調(diào)用,獲取行中所有單元格的集合 DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(colindex);//鎖定選中單元格(重點(diǎn)) if (cell != null) { TextBlock tb = cell.Content as TextBlock; Console.WriteLine(tb.Text); dgSourceData.ScrollIntoView(row, dgSourceData.Columns[colindex]); //cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(colindex); cell.Focus(); cell.Background = new SolidColorBrush(Colors.Red);//OK!問題解決,,選中單元格變色 cell.Foreground = new SolidColorBrush(Colors.Yellow); cell.FontSize = 20; } } private void dgSourceData_BeginningEdit(object sender, DataGridCellEditEndingEventArgs e) {
} /// <summary> /// 獲取父可視對象中第一個(gè)指定類型的子可視對象 /// </summary> /// <typeparam name="T">可視對象類型</typeparam> /// <param name="parent">父可視對象</param> /// <returns>第一個(gè)指定類型的子可視對象</returns> public static T GetVisualChild<T>(Visual parent) where T : Visual { T childContent = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); childContent = v as T; if (childContent == null) { childContent = GetVisualChild<T>(v); } if (childContent != null) { break; } } return childContent; }
public void dgSourceData_SelectionChanged(object sender, SelectionChangedEventArgs e) { MessageBox.Show(1111.ToString());//SelectionMode="Extended" SelectionUnit="Cell" 模式下觸發(fā)不了 } }}

前臺xaml:

<DataGrid Name="dgSourceData" AutoGenerateColumns="False" ItemsSource="{Binding StudentList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ContextMenu="{Binding menu1}" RowHeaderWidth="30" SelectedItem ="{Binding SelectedItems}" SelectionMode="Single" SelectionUnit="Cell" cal:Message.Attach="[Event SelectionChanged]=[GridControl_SelectionChanged($source,$eventArgs)];" SelectedCellsChanged="DG_SelectedCellsChanged" CellEditEnding="dgSourceData_BeginningEdit" SelectionChanged="dgSourceData_SelectionChanged" > <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{ Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="68"/> <DataGridTextColumn Header="Age" Binding="{ Binding Path=Age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="68"/> <DataGridTextColumn Header="Id" Binding="{ Binding Path=Id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="48"/> </DataGrid.Columns> </DataGrid>

04


解析

① 因?yàn)槲倚枰壎⊿electedCellsChanged事件,,所以前臺將默認(rèn)的行選中模式修改為單元格選中模式:

SelectionMode="Single" SelectionUnit="Cell"

這樣修改后SelectionChanged這個(gè)事件將不再觸發(fā)(只有在行選中模式下生效);

②獲取選中單元格的值:

var info = this.dgSourceData.SelectedCells.FirstOrDefault(); var str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;

③獲取選中的列號:

colindex = this.dgSourceData.CurrentCell.Column.DisplayIndex;//獲取選中單元格列號

④獲取選中行的行號

在SelectionUnit="FullRow" 時(shí)候:

可以通過這樣獲?。?br>

rowindex = this.dgSourceData.SelectedIndex;//獲取選中單元格行號

在當(dāng)SelectionUnit="Cell"時(shí):

我是通過選中單元格對應(yīng)行的信息和表格控件綁定的集合匹配獲取行號的:

for (int i = 0; i < ShellViewModel.StudentList.Count(); i++) { if (ShellViewModel.StudentList[i] == this.dgSourceData.CurrentItem) { //MessageBox.Show("當(dāng)前選擇的行是:" + i.ToString()); rowindex = i; } }

⑤獲取選中單元格并改變字體顏色:

var info = this.dgSourceData.SelectedCells.FirstOrDefault(); var str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text; ((TextBlock)info.Column.GetCellContent(info.Item)).Foreground = new SolidColorBrush(Colors.Red);

05


源碼下載

網(wǎng)盤下載鏈接:https://pan.baidu.com/s/1TD2BT5hiT-z-_7Z2Bn3PVQ

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多