开发者

How do you bind a DataRow to a TextBlock?

开发者 https://www.devze.com 2023-01-25 13:07 出处:网络
I have Window. Window.DataContext = DataRow. and i have TextBlock. I need to bind DataRow.Array[0] to the Text property of a TextBlock.

I have Window. Window.DataContext = DataRow.

and i have TextBlock.

I need to bind DataRow.Array[0] to the Text property of a TextBlock.

how do I do it?

edit:

<Window x:Class="Client.payment.CheckMore"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpf="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="开发者_StackOverflow社区Window_Loaded" x:Name="p_this">
            <DockPanel>
                <TextBlock x:Name="p_idCheck" Text="{Binding Path=Array[0]}"/>
            </DockPanel>
</Window>

in code: this.DataContext = dateRow.Rows[0];


The Property you're looking for is probably ItemArray. You can bind to it like this, the following ways will give you the same result (if the first Column is named Column1).

  • The first example binds to the first item in ItemArray
  • The second example binds to the value of the first Column
  • The third example binds to the value of the Column named Column1. This is the recommended approach since it will still work even if your Columns change order.

Xaml

<StackPanel>
    <TextBlock x:Name="p_idCheck"
               Text="{Binding Path=ItemArray[0]}"/>
    <TextBlock x:Name="p_idCheck2"
               Text="{Binding Path=[0]}"/>
    <TextBlock x:Name="p_idCheck3"
               Text="{Binding Path=[Column1]}"/>
</StackPanel>

Code behind example

private DataSet m_dataSet = null;
public MainWindow()
{
    InitializeComponent();
    m_dataSet = new DataSet();

    DataTable dataTable1 = new DataTable("Table1");
    dataTable1.Columns.Add("Column1", typeof(string));
    m_dataSet.Tables.Add(dataTable1);

    DataRow dataRow1 = dataTable1.NewRow();
    dataRow1["Column1"] = "Column1Value";
    dataTable1.Rows.Add(dataRow1);

    this.DataContext = dataRow1;            
}


<TextBlock Text = "{Binding Array[0]}" ...

works fine if Array is public property.

0

精彩评论

暂无评论...
验证码 换一张
取 消