开发者

How to bind a Silverlight Toolkit DataGrid cell value to a TextBlock or TextBox text property

I need to implement a master/details View, having a DataGrid as a master and quite probably TextBlock or TextBox components displaying details for each row field -- despite I'm aware about the DataForm component I'm not exactly sure that would be the solution given that currently I'm not requited to support CRUD operations over that data.

So the problem is basically how to bind each DataGridTextColumn to a particular Text property of any given control (e.g. TextBlock) having its values uptated when a different row is selected.

The XAML code would look something like this:

<!-- Master -->

<sdk:DataGrid x:Name="FoobarDataGrid" DataContext="foobar" AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn x:Name="FooDataGridColumn" Header="Foo" Binding="{Binding Foo}" />
        <sdk:DataGridTextColumn x:Name="BarDataGridColumn" Header="Bar" Binding="{Binding Bar}" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

<!-- Details -->

<TextBox x:Name="FooDetailsTextBlock" Text="{Binding <!-- TODO -->}" />
<TextBox x:Name="BarDetailsTextB开发者_如何学运维lock" Text="{Binding <!-- TODO -->}" />

Don't hesitate to ask for any other detail as every bit of advice would be much appreciated.


EDITED: Here is what your code would look like...

<!-- Master -->

<sdk:DataGrid x:Name="FoobarDataGrid" DataContext="foobar" AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn x:Name="FooDataGridColumn" Header="Foo" Binding="{Binding Foo}" />
        <sdk:DataGridTextColumn x:Name="BarDataGridColumn" Header="Bar" Binding="{Binding Bar}" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

<!-- Details -->

<TextBox 
    DataContext="{Binding SelectedItem, ElementName=FoobarDataGrid}"
    x:Name="FooDetailsTextBlock" 
    Text="{Binding Foo}" />

<TextBox 
    DataContext="{Binding SelectedItem, ElementName=FoobarDataGrid}"
    x:Name="BarDetailsTextBlock" 
    Text="{Binding Bar}" />

Original Answer...

Here is a way...

My DataGrid is bound to a collection of a custom class. That class has a property called LabName. This is what I bind to a column in the DataGrid

XAML

    <sdk:DataGrid 
        Name="LabsDataGrid" 
        ItemsSource="{Binding}" 
        AutoGenerateColumns="False" 
        Height="284" 
        HorizontalAlignment="Left" 
        Margin="205,250,0,0" 
        VerticalAlignment="Top" 
        Width="359" 
        IsReadOnly="True">

        <sdk:DataGrid.Columns>

            <sdk:DataGridTextColumn 
                Header="Lab Name" 
                Binding="{Binding LabName}" />

        </sdk:DataGrid.Columns>

    </sdk:DataGrid>


    <TextBox 
        DataContext="{Binding SelectedItem, ElementName=LabsDataGrid}"
        Name="LabName" 
        Text="{Binding LabName}"
        Height="23" 
        HorizontalAlignment="Left" 
        Margin="324,121,0,0" 
        VerticalAlignment="Top"
        Width="132" />

Notice that I am setting the DataContext of the TextBox to the SelectedItem of the DataGrid. So when the user clicks a row in the DataGrid, the TextBox is populated with the LabName that is bound to the column in the DataGrid.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜