Question

I want to bind TextBlock's Text property to some elements' and some model's properties. Something like this:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding>
            <Binding ElementName="myElement1" Mode="OneWay" Path="Text" />
            <Binding ElementName="myElement2" Mode="OneWay" Path="Text" />
            <Binding Mode="OneWay" Path="Property1" />
            <Binding Mode="OneWay" Path="Property2" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

The TextBlock has a text value, combination of myElement1, myElement2 and Property1, Property2. There is not a problem. The text value is generated successfully.

Here is my question:

Can I bind whole (combined) text value of the TextBlock to another model's property, i.e. Property3, without code?

Was it helpful?

Solution

Not without some really bad hacking which would require writing some code to set up attached properties and other bindings anyway. The issue is that any binding has 2 ends: target and source. Since the target (where the binding is set) must be a DependencyProperty that means that your model must be on the source end of the binding you're trying to do. This isn't a problem as far as setting a value since TwoWay and OneWayToSource bindings do this just fine.

You have a bigger problem though in that the original place where the value is coming from (TextBlock.Text) already is assigned a binding and so can't be the target for your model binding. You might next want to try using another UIElement property as an intermediary to take the Text value and push it to the model. To do that you again need the model to be the source and the other UIElement property to be the target. But that same property also needs to be the target of a binding to the original Text property that you're trying to extract, so again you're stuck.

Bottom line is that you're much better off handling this in your Model and ViewModel layers rather than trying to force the stuff you have set up in XAML to be driving everything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top