2
votes

EDIT

I've corrected the binding issue as quoted in the Output Window:

System.Windows.Data Information: 41 : BindingExpression path error: 'Source{x:Static SystemCommands' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

I'd missed a = between Source and {

This was mentioned in every line of the output window as quoted below, however fixing this has not corrected the problem that the buttons still do nothing.

The Output Window is now chock full of the following, all referencing controls that are not my window buttons:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')


ORIGINAL QUESTION

Based on a few answers on SO, I've built the following buttons to close, maximize, restore and minimize my window:

<!--Close Button-->
<Button x:Name="Close" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25">
        <Path x:Name="Cross" Data="M0,0 L1,1 M0,1 L1,0" Stretch="Fill" Width="8" Height="8"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2" />
    </Grid>
</Button>

<!--Maximize Button-->
<Button x:Name="Maximize" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" >
    <Grid Width="30" Height="25">
        <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2"  />
    </Grid>
</Button>

<!--Restore Button-->
<Button x:Name="Restore" Visibility="Collapsed" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">
        <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="1"  />
    </Grid>
</Button>

<!--Minimize Button-->
<Button x:Name="Minimize" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25">
        <Path Data="M9,0 L8,1 8,1 8,1 0,1 0,1 z" Width="9" Height="9" VerticalAlignment="Bottom" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2" />
    </Grid>
</Button>

I've checked the bindings with some other developers who are more knowledgeable and they say the bindings look fine but maybe I need a namespace. They had to go before they were able to clarify.

The Output Window in Visual Studio shows the following errors (paradoxically, all seem to be relevant to the Close Window Button which is the only one that has it's style TitleButton working on it.

Seems like there's some sort of data binding problem here which is strange since I obviously don't need to bind these to data... I did find a few answers on SO but all seem to deal with other things:

System.Windows.Data Information: 41 : BindingExpression path error: 'Source{x:Static SystemCommands' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

1

1 Answers

1
votes

When using the static routed commands found in the static SystemCommands class you do not need to use a Binding and can instead simplify your xaml to the following.

Command="{x:Static SystemCommands.MinimizeWindowCommand}"