1

I want to create a Button that changes by a bool and isMouseOver value.

  • player is playing -> Pause_grey.png
  • player is playing & mouseIsOver-> Pause_blue.png
  • player is stopped -> Play_grey.png
  • player is stopped & mouseIsOver -> Play_blue.png

I want to do this with 4 MultiDataTrigger but when the playStatus (boolean value of dataContext) changes the button is not recognizing it

Code:

Button:

<Button Name="btnPlayPause" Grid.Column="2" Grid.Row="0" Click="musikPlayer_btnClick" HorizontalAlignment="Center" Style="{StaticResource PlayPauseButton}"></Button>

Style of Button:

<Style TargetType="{x:Type Button}" x:Key="PlayPauseButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <StackPanel Orientation="Horizontal" >
                                <Image Name="PART_Image" Source="graphics/Pause_grey.png" VerticalAlignment="Center" RenderOptions.BitmapScalingMode="HighQuality" ></Image>
                            </StackPanel>
                            <ControlTemplate.Triggers>



                                <MultiDataTrigger >
                                    <MultiDataTrigger.Conditions>
                                        <!--is not playing and mouse is not over-->
                                        <Condition Binding="{Binding playStatus}" Value="false"/>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="false" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Source" Value="graphics/Play_grey.png" TargetName="PART_Image"/>
                                </MultiDataTrigger>



                                <MultiDataTrigger >
                                    <MultiDataTrigger.Conditions>
                                        <!--is not playing and mous is over-->
                                        <Condition Binding="{Binding playStatus}" Value="false"/>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Source" Value="graphics/Play_blue.png" TargetName="PART_Image"/>
                                </MultiDataTrigger>




                                <MultiDataTrigger >
                                    <MultiDataTrigger.Conditions>
                                        <!--is playing and mous is not over-->
                                        <Condition Binding="{Binding playStatus}" Value="true"/>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="false" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Source" Value="graphics/Pause_grey.png" TargetName="PART_Image"/>
                                </MultiDataTrigger>


                                <MultiDataTrigger >
                                    <MultiDataTrigger.Conditions>
                                        <!--is playing and mouse is over-->
                                        <Condition Binding="{Binding playStatusy}" Value="true"/>
                                        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Source" Value="graphics/Pause_blue.png" TargetName="PART_Image"/>
                                </MultiDataTrigger>



                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

So anyone know how to get this working? :)

EDIT1:

I don't know how, or if I can setup INotifyPropertyChanged because of the value:

public Boolean playStatus { get { return !model.getMusicPlayer().getStream1_pause(); } }

EDIT2: I tried this but it doesnt work

   protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public bool stream1_pause
    {
        get { return Stream1_pause; }
        set
        {
            if (value != Stream1_pause)
            {
                Stream1_pause = value;
                OnPropertyChanged("stream1_pause");
            }
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

Maybe I should call update manually like I can do in listView, but I don't know how to update the conditions. In ListView it looks like:

BindingOperations.GetBindingExpressionBase(songs, ListView.ItemsSourceProperty).UpdateTarget();

So I tried:

 BindingOperations.GetBindingExpressionBase(btnPlayPause, Button.StyleProperty).UpdateTarget();

but it says:

System.NullReferenceException

.... the question is why?

Fjodr
  • 919
  • 13
  • 32
Kilian E.
  • 11
  • 3
  • 1
    Did you set the `Window`'s `DataContext`? `DataContext="{Binding RelativeSource={RelativeSource Self}}"` – Physikbuddha May 28 '15 at 11:52
  • The `playStatus` property needs to either be backed by a `DependencyProperty` or the class it is in needs to implement `INotifyPropertyChanged` and the setter needs to fire the `PropertyChanged` event. Are you doing one of those? – Xavier May 28 '15 at 12:11
  • the thing is the playStatus in the context returns a value of another class, so i think i cant set a `INotifyPropertyChanged ` in the set state: `public Boolean playStatus { get { return !model.getMusicPlayer().getStream1_pause(); } } ` – Kilian E. May 28 '15 at 12:42
  • 1
    Then it will have to find out, somehow, when the music player stops playing, and then fire a `PropertyChanged` event for `playStatus`. Should be easy to do if the music player raises events when its status changes. – Pieter Witvoet May 28 '15 at 13:32
  • ... hm this doesn't work like expected: see edit 2 – Kilian E. May 28 '15 at 14:01
  • 1
    You are binding to the property playStatus but raising the event for stream1_pause. You need to raise the event with "playStatus" as the parameter as Pieter stated.. – apc May 28 '15 at 14:05
  • this didn't help ... i just use toggle button know ... maybe sometimes i try this again :D – Kilian E. May 28 '15 at 20:03

0 Answers0