0

I have a TextBlock with two MultiBindings. I'd like to make a user control out of this, as I need several instances. The only difference amongst the instances is the Name, and that is only needed as a sort of parameter to the MultiBinding.

<TextBlock x:Name="That"><
  TextBlock.Foreground>
    <MultiBinding Converter="{StaticResource multiValueFgColorConverter}">
        <Binding ElementName="That" Path="Name" />
        <Binding Path="TimerState" Mode="TwoWay" />
        <Binding Path="Which" Mode="TwoWay" />
    </MultiBinding>
</TextBlock.Foreground><TextBlock.Opacity>
    <MultiBinding Converter="{StaticResource multiValueOpacityConverter}">
        <Binding ElementName="That" Path="Name" />
        <Binding Path="TimerState" Mode="TwoWay" />
        <Binding Path="Which" Mode="TwoWay" />
    </MultiBinding>
</TextBlock.Opacity><Bold><Run Text="That"/></Bold>

-- Is there a way to inject the Name in to an instance of the user control?
-- Or, perhaps wrap this user control in another control where it is used, and have the UC inherit the parent's name?

<grid x:Name="That">
    <my:UC/>  <!-- 'Inherits' "That"? -->
</grid>   

-- Alternatively, is there a way to pass a string as a parameter to the MultiBinding? MultiBinding doesn't take ConverterParameters, as far as I know, so it has to be fudged through the Binding (although I am probably unaware of the better way...)
Thanks for any insights --

Number8
  • 12,322
  • 10
  • 44
  • 69

2 Answers2

0

Within UserControl expose dependency property and within MultiBinding bind to it. Every single instance of UserControl can then assign binding to specific property making multiBinding updated.
As far as inheriting is concerned, you can inherit propertie's value as long as it allows to do so. x:Name is not able to be inherited.

Maximus
  • 3,458
  • 3
  • 16
  • 27
  • If I understand you correctly: Add a Dependency Property to the UC, and bind to that in the MultiBinding. What is the syntax for that - binding to a DP on the control? The datacontext for the control is a ViewModel; the other Binding items refer to Properties in the ViewModel class. Thanks for the reply. – Number8 Dec 29 '15 at 21:02
  • Assign ViewModel to top UI element's DataContext instead of UserControl. Then you can refer to UserControl's DataContext with binding set to RelativeType of UserControl or set x:Name to UserControl and refer to it with binding ElementName. If you do not change ViewModel assignment than your DP will bind to UserControl's ViewModel not external ViewModel where intended property resides. – Maximus Dec 29 '15 at 21:06
  • In order to get brighter insight take a look, http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html – Maximus Dec 29 '15 at 21:11
0

It's been a while since I touched WPF, can't you use Relative source for that ? See below. How do I use WPF bindings with RelativeSource?

Community
  • 1
  • 1
restrada
  • 83
  • 4
  • Thanks, I finally figured out the RelativeSource. Added a Dependency Property called ThisName to the UC, and bound to it like this in the MultiBinding: When I create instances of the UC, I set the ThisName property, and Bob's your uncle... – Number8 Dec 29 '15 at 22:43