0

I have a collection of the object say MyModel.cs. The collection is called Some. In MyModel it has a property Color which is Brush type.

The Color is Red for all now.

Now I have

<MyControl: x:Name="control1" ItemsSource="{Binding Some}" Stroke="Red">

But you see I hardcode the stroke. What I want is something like Stroke = "{Binding Some.Color}"

How?

Bigeyes
  • 1,508
  • 2
  • 23
  • 42
  • You will need either use MultiBinding with custom converter (to resolve case when two models from collection has different colors - which to choose?) or move property to the upper level (where collection lives). – Taras Shcherban Nov 16 '17 at 15:23
  • @Shcherban, if there are different colors then I have different collections. So I don't use MultiBinding. – Bigeyes Nov 16 '17 at 15:30
  • Okay, then you can use just regular binding and pick color from first item in collection, like `Stroke={Binding Some, Converter=SomeConverter}` and in covnerter as simple as `return (value as IEnumerable)?.FirstOrDefault()?.Color ?? Colors.Red` – Taras Shcherban Nov 16 '17 at 15:32
  • ok so you are looking something like [this](https://stackoverflow.com/a/5647426/2470362), as Shcherban mentioned Multibinding or move it to upper level are other options. – Abin Nov 16 '17 at 15:34
  • 1
    Does MyControl have a SelectedItem like a ListBox? – 15ee8f99-57ff-4f92-890c-b56153 Nov 16 '17 at 15:38
  • 2
    If MyControl has an ItemsSource property, it should also have an ItemTemplate. Declare a DataTemplate with an element that binds (e.g. its Background property) to Color. – Clemens Nov 16 '17 at 15:39
  • 1
    Are you trying to set each item's Stroke separately, or are you trying to set one Stroke for the whole `MyControl`? There is a difference, it's an important difference. You *must* brace yourself, be very brave, and provide that information. – 15ee8f99-57ff-4f92-890c-b56153 Nov 16 '17 at 15:46
  • 1
    I am trying to set one Stroke for the whole MyControl@EdPlunkett – Bigeyes Nov 16 '17 at 15:52

2 Answers2

2

The control displays several models. If you want to set the Stroke property to the Color of one of them, you need to specify which one, e.g.:

<MyControl x:Name="control1" ItemsSource="{Binding Some}" Stroke="{Binding Some[0].Color}">

This will bind the Stroke property to the Color property of the first MyModel object, assuming that the Some collection has an indexer.

mm8
  • 163,881
  • 10
  • 57
  • 88
1

If MyControl inherits from Selector, set IsSynchronizedWithCurrentItem="True" and bind to Some/Color. The slash or virgule (/) means to use the Color property from the "current item" in the collection:

<MyControl 
    IsSynchronizedWithCurrentItem="True"
    x:Name="control1" 
    ItemsSource="{Binding Some}" 
    Stroke="{Binding Some/Color}"
    />

This will use the selected item's Color brush for Stroke. You're quite understandably reluctant to provide any hints about what you're trying to do, but that's a fair guess.

If MyControl does not inherit from Selector, use mm8's solution.