Link ListBox Borderbrush 属性 到 IsEnabled 按钮

Link a ListBox Borderbrush property to a IsEnabled button

我有两个 TextBoxes、两个 ListBoxes、一个 Cancel 按钮和一个 OK 按钮。

简化问题,我想link把第二个ListBoxBorderbrush的颜色改成IsEnabled属性的OK按钮。

另一种方法是 link 颜色更改为 ListBoxItem 背景而不是 Listbox 边框本身。

是否可能(可能通过 Triggers 或其他方式)?如果是这样,你能给我指路吗?

window的XAML如下:

<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>

这是一个小例子,它根据按钮的 IsEnabled 属性 更改列表视图的边框刷

<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>

但我会在命令上设置按钮的可用性,而不是在 XAML 中,我还会将 ListView 的颜色绑定到 ViewModel 中的 IsValid 属性。