WPF Learning Memo (3) data processing in a DataGrid

Source: Internet
Author: User
Tags bind

How to get the contents of a cell from a Datagrid conversion of bound data using a value converter ivalueconverter

How to get the contents of a cell from a Datagrid

The DataGrid belongs to a itemscontrol, so it has the Items property and encapsulates its items with Itemcontainer.

However, the DataGrid in WPF differs from the DataGridView in Windows Forms. In the Items collection of the DataGrid, DataGridRow is an item, but the cells inside it are encapsulated in a datagridcellspresenter container; We can't use statements like DataGridView.Rows.Cells to get the contents of a cell. However, in WPF we can go into the control "inside" through the visual tree (VisualTree), then of course we can go through visualtree into the DataGridRow and Datagridcellspresenter in the DataGrid, And get an example in Datagridcellspresenter, you can iterate through the following code VisualTree

DataGridRow 

Rowcontainer = (datagridrow) dataGrid1.ItemContainerGenerator.ContainerFromIndex (rowIndex);
Datagridcellspresenter presenter = getvisualchild<datagridcellspresenter> (RowContainer);
DataGridCell cell = (DataGridCell) presenter. Itemcontainergenerator.containerfromindex

(columnindex);
     
// ...
     
public static T getvisualchild<t> (Visual parent) where t:visual
{
  t child = Default (t);
  int numvisuals = Visualtreehelper.getchildrencount (parent);
     
  for (int i = 0; i < numvisuals i++)
  {
    Visual v = (visual) visualtreehelper.getchild (parent, i);
    Child = V as T;
       
    if (child = = NULL) Child
      = getvisualchild<t> (v);
    else break
      ;
  }
     
  return child;
}

II. WPF uses a value converter to bind data to a transform IValueConverter

Sometimes, we want to have the bound data displayed in another format, or convert it to another type, and we can use a value converter to do it. For example, I saved a file path "C:\abc\abc.exe" in my data, but I wanted him to appear as "Abc.exe" in the foreground list. First we build a class of ivalueconverter interfaces.

The class Getfilename:ivalueconverter  
{  
    //convert method is used to convert data into the format that we want to display public  
    object Convert (object value, Type targettype, object parameter, CultureInfo culture)  
    {  
        FileInfo fi = new FileInfo ((string) value);  
        return FI. Name;  
    }  
    The Convertback method converts the display value to its original format, because I do not need a reverse conversion, so I throw an exception public  
    object Convertback (object value, Type targettype, Object parameter, CultureInfo 

culture)  
    {  
        throw new notimplementedexception ();  
    }  
}

In order to use this converter, we will map the project's namespace to XAML, such as my project name is Automatic Update, with local as the space name prefix

Xmlns:local= "Clr-namespace: Namespaces"

To make it easier to use, we create a converter object in the Resources collection

<Window.Resources>  
    <local:getfilename x:key= "GetFileName" ></local:GetFileName>  
< /window.resources>

Now we go to the place where we bind the data using StaticResource to point to the converter

<TextBlock>  
    <TextBlock.Text>  
        <binding path= "FileName" >  
            <Binding.Converter>  
                <local:GetFileName></local:GetFileName>  
            </Binding.Converter>  
        </Binding>  
    </TextBlock.Text>  
</TextBlock>

or use this:

<textblock text= "{Binding 

path=filename,converter={staticresource GetFileName}}"  />

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.