0

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?

2 Answers 2

1

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 Silverlight 4 Equivalent to WPF "x:static"

1
  • I get "System.Windows.Data.Binding" as my header using this approach.
    – PhilBrown
    Oct 28, 2010 at 12:46
0

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

Dynamically setting the Header text of a Silverlight DataGrid Column

2
  • well can you do that and then try my solution?
    – Alex Lo
    Oct 28, 2010 at 16:37
  • I tried your solution, I got "System.Windows.Data.Binding" as my header using that approach.
    – PhilBrown
    Oct 28, 2010 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.