lucas.mdo
lucas.mdo

Reputation: 439

Link a ListBox Borderbrush property to a IsEnabled button

I have two TextBoxes, two ListBoxes, a Cancel button and an OK button.

Simplifying the problem, I would like to link the color of the Borderbrush of the second ListBox to the IsEnabled property of the OK button.

An alternative would be link that color change to the ListBoxItem background instead of the Listbox border itself.

Is it possible (maybe through Triggers or something)? If so, could you show me the way?

The XAML of the window is as follows:

<Window x:Class="Opt.ExpertSystem.View.WindowPasteRules"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:scroll="clr-namespace:Opt.ExpertSystem.View"
        Title="Paste Rules Options"  Width="400" Height="300">
    <Window.InputBindings>
        <KeyBinding Key="Esc" Command="{Binding CancelCommand}" />
    </Window.InputBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28"/>
            <RowDefinition Height="100*"/>

            <RowDefinition Height="35"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="76*" />
        </Grid.ColumnDefinitions>

        <Label Content="Select Rules to Paste: " Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" HorizontalContentAlignment="Right" Margin="0,0,2,0" Width="Auto"/>

        <Grid Grid.Row="1" Grid.ColumnSpan="2"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <Grid.Resources>
                <Style TargetType="ScrollViewer">
                    <Setter Property="scroll:ScrollSynchronizer.ScrollGroup" Value="Group1" />
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="50*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="5" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Label Content="Replace" HorizontalAlignment="Stretch" FontWeight="Bold"/>
            <TextBox Margin="55,2,2,2" Text="{Binding CopyNameOrigin}" ToolTip="Non-editable field. Represents the text you want to replace." Focusable="False"/>
            <Label Content="With" Grid.Column="2" HorizontalAlignment="Stretch" FontWeight="Bold"/>
            <TextBox Grid.Column="2" Margin="42,2,2,2" Text="{Binding CopyNameNew, UpdateSourceTrigger=PropertyChanged}" ToolTip="Represents the results of the changes to be made." />
            <ListBox ItemsSource="{Binding Path=CopiedRules}" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" BorderThickness="1" BorderBrush="CornflowerBlue" Grid.IsSharedSizeScope="True" Margin="2,0,2,10" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Name}" />    
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <ListBox ItemsSource="{Binding Path=PasteRules}" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2" BorderThickness="1" BorderBrush="CornflowerBlue"  Margin="2,0,2,10" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="2,5,2,5" />             
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

        <Grid Grid.Row="2" Background="#FFECE9D8" Grid.ColumnSpan="2">
            <Button Content="OK" x:Name="btnOK" IsEnabled="{Binding IsValid, UpdateSourceTrigger=PropertyChanged}" Margin="0,6,6,0" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Height="23" Click="btnOK_Click" />
            <Button Content="Cancel" x:Name="btnCancel" IsCancel="True" Margin="0,6,90,0" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Height="23" />
        </Grid>
    </Grid>
</Window>

Upvotes: 0

Views: 77

Answers (1)

The One
The One

Reputation: 4784

Here's a small example that changes the border brush of a listview based on the IsEnabled property of a button

<StackPanel>
    <ListBox Height="100">
        <ListBox.Style>
            <Style TargetType="ListBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=okButton, Path=IsEnabled}" Value="false">
                        <Setter Property="BorderBrush" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=okButton, Path=IsEnabled}" Value="true">
                        <Setter Property="BorderBrush" Value="Blue"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
    <Button IsEnabled="True" Name="okButton">true</Button>
</StackPanel>

But I would set the availability of the button on the command and not in the XAML, also I would bind the color of the ListView to the IsValid property in the ViewModel instead.

Upvotes: 1

Related Questions