Edit DataGridTemplateColumn in Code Behind?
I would think that this would be incredibly simple, but I've searched all over and can't seem to find an answer. I have a DataGridTemplateColumn
that I want to use to display a value that isn't in the DataContext
of the DataGrid
. I.e. I have an entity that has different names based on the culture. When the grid loads, I want to go get the appropriate name based on the current culture. Every time I see anything about DataGridTemplateColumn
s, they're always using the Binding syntax. I can't do that here. What C# code do I need to access the "nameValue" TextBlock in the following XAML, and in what event handler should I access it:
<Datagrid:DataGridTemplateColumn Header="Name" x:Name="nameField">
<Datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="nameValue" />
</StackPanel>
</DataTemplate>
</Datagrid:DataGridTemplateColumn.CellTemplate>
</Datagrid:DataGridT开发者_C百科emplateColumn>
Thanks to all in advance and I'm sorry for the super n00b question.
You can still use the binding syntax, it sounds like you just need to bind to a static method instead of the data context of the grid. There is a good reference here http://blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html Taking that as an example and modifying for your case.
First: Setup your grid as you would normally, item source and columns, standard data binding. This will take care of any columns you need from the database or other source.
Second: In your project add your static class
namespace StaticBinding
{
public class MyStaticClass
{
private static string myStaticProperty;
public static string MyStaticProperty
{
get
{ return
(CultureInfo.CurrentCulture.Name == "en-US" ? "US" : "Other");
}
set { myStaticProperty = value; } }
}
}
Third: Add your new resource to app resources
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SilverlightApplication28.App"
xmlns:myns="clr-namespace:StaticBinding"
>
<Application.Resources>
<myns:MyStaticClass x:Name="MyStaticClass"></myns:MyStaticClass>
</Application.Resources>
Finally: Set the binding in your TextBlock
, if you've built your project, you should be able to see the property in the binding editor window.
<sdk:DataGrid AutoGenerateColumns="False" Height="171" HorizontalAlignment="Left" Margin="61,53,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="263" ItemsSource="{Binding Collection}" LoadingRow="dataGrid1_LoadingRow" Loaded="dataGrid1_Loaded" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Property1}" Header="Property1"/>
<sdk:DataGridCheckBoxColumn Binding="{Binding Property2}" Header="Property2"/>
<sdk:DataGridTextColumn Binding="{Binding Property3}" Header="Property3"/>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="nameValue" Text="{Binding Source={StaticResource MyStaticClass}, Path=MyStaticProperty}" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
精彩评论