views:

26

answers:

2

Say I have a bunch of local constants in my code behind that I want to use as headers, for example:

const string TYPE_HEADER = "Type";
const string LOCATION_ HEADER = "Location";

etc.

Is there any way I can bind the headers of my DataGridColumns to these like events are bound to local methods, for example:

<data:DataGridTextColumn Header="{Binding TYPE_HEADER}" />

Can this be done? Perhaps by using some dynamic ResourceDictionary or something?

A: 

the TYPE_HEADER must be a string property (it can be backed by a const). make a container:

public class MyStaticDataProvider
{
public string TYPE_HEADER { get { return "blkajsd"; } }
}

below the declaration of your usercontrol:

<UserControl.Resources>
<ResourceDictionary>
 <MyNamespace:MyStaticDataProvider x:Key="NameProvider" />
</ResourceDictionary>
</UserContro.Resources>

for your header:

Header="{Binding Path=TYPE_HEADER, Source={StaticResource NameProvider}, Mode=OneTime}"

it would be easier if silverlight supported x:Static, but it does not. see http://stackoverflow.com/questions/3373926/silverlight-4-equivalent-to-wpf-xstatic

Alex Lo
I get "System.Windows.Data.Binding" as my header using this approach.
PhilBrown
A: 

It appears this cannot be done without editing the control template for DataGridTextColumn as Header is not a FrameworkElement...

http://stackoverflow.com/questions/151682/dynamically-setting-the-header-text-of-a-silverlight-datagrid-column

PhilBrown
well can you do that and then try my solution?
Alex Lo
I tried your solution, I got "System.Windows.Data.Binding" as my header using that approach.
PhilBrown