Binding Boder.Background to LinearGradientBrush from Resource Dictionary?

You can't do exactly what you're trying to do, which is essentially to use data binding to store the key of a resource within your bound object. The closest you can get would be the answer to this question which basically uses a ValueConverter to get the resource from the application's resources.

Up vote 0 down vote favorite share g+ share fb share tw.

I'd like to Bind the Background of a Border Property to elements in a list. I have a Dictionary holding the follwing: Now I fill a ObservableCollection holding Objects that contain a Property called "BackgroundStyle". When I fill a list box with styled background I'd like to bind the Background to "BackgroundStyle" This works well, if BackgroundStyle="Red" or "Green" but it won't work if I use "ConfigurationItemBackground".

Any suggestions? Thanks for your help ;) -Tim- c# wpf xaml data-binding link|improve this question asked Oct 24 '11 at 22:03tim.1.

– Philipp Schmid Oct 24 '11 at 22:42 @PhilippSchmid: His ListBox has that collection as its ItemsSource, it sounds like. The Border looks like it's the DataTemplate, and he's binding Background to the BackgroundStyle property. – Adam Robinson Oct 24 '11 at 22:45.

You can't do exactly what you're trying to do, which is essentially to use data binding to store the key of a resource within your bound object. The closest you can get would be the answer to this question, which basically uses a ValueConverter to get the resource from the application's resources. Unfortunately, this won't work with what you want to do, which is use a local resource.

For that, you'd be better off writing your own custom ValueConverter that converts your string value into a brush. You could do something generic like this: ValueConversion(typeof(string), typeof(object)) public class ResourceKeyConverter : IValueConverter { public ResourceKeyConverter() { Resources = new ResourceDictionary(); } public ResourceDictionary Resources { get; private set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Resources(string)value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new InvalidOperationException(); } } Then use it like this: ... (I'm at home and don't have Visual Studio in front of me, so this might require some tweaking, but it should be largely correct).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions