链接一个ListBox Borderbrush属性到一个IsEnabled按钮

本文关键字:一个 IsEnabled 按钮 Borderbrush 链接 ListBox 属性 | 更新日期: 2023-09-27 18:08:51

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

简化问题,我想将第二个ListBoxBorderbrush的颜色链接到OK按钮的IsEnabled属性。

另一种方法是将颜色更改链接到ListBoxItem背景,而不是Listbox边框本身。

是可能的(也许通过Triggers或其他东西)?如果是这样,你能给我指路吗?

窗口的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>

链接一个ListBox Borderbrush属性到一个IsEnabled按钮

下面是一个小示例,它基于按钮的IsEnabled属性来改变listview的边框刷

<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属性。

相关文章: