0

I'm super new to WPF and MVVM, and I need to execute a command commandToExec in my View as soon as my ViewModel sets a property propertyToSet to true. Right now, I have a Stackpanel that becomes visible when propertyToSet is set, and a button inside that Stackpanel that is bound to the command commandToExec, and a Datatrigger on that button that presses it if propertyToSet is set. Something that looks roughly like this:

       <StackPanel Visibility="{Binding propertyToSet, Converter={StaticResource BooleanToVisibilityConverter}}">
            <Button
                Command="{Binding commandToExec}">
                <Style TargetType="Button">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding commandToExec, Converter={StaticResource BooleanToVisibilityConverter}}">
                            <Setter Property="Button.IsPressed" Value="True" />   //Trying to set button isPressed to true to trigger command
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button>
        </StackPanel>

This doesn't work, and seems unintuitive and clunky, so I'm sure there is a better way to do this. I don't know much about XAML, so I'm just ballparking this code, but I would like to do something like this:

        <StackPanel Visibility="{Binding propertyToSet, Converter={StaticResource BooleanToVisibilityConverter}}">
              <SomethingThatAutomaticallyDoesCommand Command="{Binding commandToExec}"/>
        <StackPanel/>

So that as soon as StackPanel becomes visible via propertyToSet, the arbitrary object SomethingThatAutomaticallyDoesCommand does its job and runs the command. Another hacky idea I had saw to just set a tag on the StackPanel that updates it based on propertyToSet, and somehow binding my command to TargetUpdated, as found [here][1].

Anyway, I hope that made sense. Thanks so much! [1]: Can i call a function via DataTrigger (XAML)?

wotocem
  • 141
  • 1
  • 1
  • 5
  • Why so complicated? From your bindings I can see that the `propertyToSet` and `commandToExec` are in the same view model. Simply call the execute delegate from the set method of the `propertyToSet` property. – BionicCode Oct 03 '20 at 14:36
  • Okay, so when propertyToSet is called, just execute commandToExec in the set method? That makes sense, but unfortunately I just realized commandToExec comes from a parent view, so when I bind it in my view, I have to use `{Binding DataContext.commandToExec, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Mode=OneWay}`. I don't think I can access commandToExec from this viewModel now. Sorry for the confusion, these are changes I've made since posting. – wotocem Oct 03 '20 at 17:19
  • Please, update your question to show the **full context**. You must show all relevant views and how they are working together. How/when is the `StackPanel` loaded etc. Otherwise it is not possible to help. Without knowing any details, it seems that your design may have some flaws. Consider to move `propertyToSet` to the same view model that exposes the command. Again, please update your question. – BionicCode Oct 03 '20 at 18:05
  • Don't forget to show the `DataContext` of all views. Why does the child view has a different `DatContext`? It appears both views depend on the same data context. – BionicCode Oct 03 '20 at 18:09

0 Answers0