Question

I have a WPF application using custom control that I have created. Within the control I have defined a number of VisualStates within a VisualStateGroup. Outside of the custom control, but within the same application, I have some buttons that I want to change the displayed VisualState of the custom control. Can this be done without writing code?

Was it helpful?

Solution

Yes you can change a state of a custom control from outside by just using XAML:

<Button Content="Button" 
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ei:GoToStateAction TargetObject="{Binding ElementName=myControl}" StateName="SomeState"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

However avoid it as much as possible. Usually a control should manage its own state. You can for exemple expose a few properties in your Custom control that will trigger the states (in code or by Trigger/Action in the style of the control). Switching states from outside reduces the visibility of your code.

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