How i can change button's IsEnabled property from DataTrigger on ListBox style?

How can I change button's IsEnabled property from DataTrigger on ListBox style? I try so many options, and all the same, I don't have a solution.

<Style TargetType="ListBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Value="0">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border BorderThickness="0 0 0.55 0.55" BorderBrush="black" Margin="0 0 0 5">
                                    <TextBlock Foreground="Black" Text="Нет студентов" Margin="20" FontSize="11"></TextBlock>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
</Style>

<Grid x:Name="gridButtons" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.1*" />
                <ColumnDefinition Width="0.1*" />
                <ColumnDefinition Width="0.1*" />
                <ColumnDefinition Width="0.2*" />
            </Grid.ColumnDefinitions>
            <Button Command="{Binding AddCommand}" Grid.Column="0">
                <Image Source="Resources/icons/add-icon.png"></Image>
            </Button>
            <Button Command="{Binding RemoveCommand}" CommandParameter="{Binding SelectedStudent}" Grid.Column="1">
                <Image Source="Resources/icons/delete-icon.png"></Image>
            </Button>
            <Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
                <Image Source="Resources/icons/save-icon.png"></Image>
            </Button>
            <Button Command="{Binding RefreshCommand}" Grid.Column="3">
                <Image Source="Resources/icons/refresh-icon.png"></Image>
            </Button>
        </Grid>

I need when ListBox items count is 0, that btnSave's property IsEnabled is True.

I did all as mm8 wrote me and that still doesn't works.

What did i?

<ListBox x:Name="lbStudents" Grid.Column="0" AlternationCount="2" BorderThickness="0 0 1 1" BorderBrush="black" Margin="0 0 0 5" ItemsSource="{Binding Students}"
                     SelectedItem="{Binding SelectedStudent}" SelectionMode="Single">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="stpListBoxItem" Margin="4 7 4 7">
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding StringFormat=" {0} {1}">
                                        <Binding Path="LastName"/>
                                        <Binding Path="FirstName"/>
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
</ListBox>

<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
                    <Image Source="Resources/icons/save-icon.png"></Image>
                    <Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Items.Count, ElementName=lbStudents}" Value="0">
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
</Button>

I spent very much time to this, but still don't found of the solution. Sorry for my bad English.

Upvotes: 0

Views: 1217

Answers (1)

mm8
mm8

Reputation: 169160

How can I change button's IsEnabled property from DataTrigger on ListBox style?

You can't. A Setter in a ListBox Style can only set properties of the ListBox to which the Style is being applied.

You could apply a Style to the Button that binds to a property of the ListBox though:

<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
    <Image Source="Resources/icons/save-icon.png"></Image>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="IsEnabled" Value="False" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Items.Count, ElementName=listBox}" Value="0">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

This only works if the ListBox with an x:Name of "listBox" is in the same naming scope as the Button.

What you really should do is to return false from the CanExecute method of your SaveCommand when the source collection that the ItemsSource property of the ListBox is bound to has a count of 0.

Upvotes: 2

Related Questions