How do I bind a control to visualbrush in MVVM?

I have a visualbrush where I want to set a visual. Current example of my visual brush:

<VisualBrush Viewport="0,0,0.5,0.5" Visual="{Binding ElementName=Panel}"></VisualBrush>

      

However, I want to change the value in the Visual so that it can display the visuals of different controls at different times. My first idea was this:

Visual="{Binding ElementName={Binding VisElName}}"

      

But first this is not allowed and secondly my viewmodel needs to know the name of my items. Is there a way to do this in MVVM so that I can change the visibility to a different element at will?

+3


source to share


1 answer


It depends on what the actual logic is driving the visual change. For example, if it is some kind of state related to business logic, you can open a property State

in your view model and use a converter to bind to it.

If it doesn't depend on the BL state, it would be ok to MVVM to modify the visual from behind view code as it was considered purely view logic:

XAML



<VisualBrush x:Name="myBrush" Viewport="0,0,0.5,0.5" />

      

Code for

myBrush.Visual = myPanel;

      

0


source







All Articles