4

我正在尝试启用和禁用Button基于ListBox. 如果选择了一个值,则应启用该按钮。
我正在尝试使用触发器,但它给了我一个错误

'目标名称无法识别'。

<UserControl x:Class="Qualification.View.QualificationView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Height="500" Width="500">
    <Grid>
        <Button Name="btnOpen"  Command="Open" CommandParameter="{Binding ElementName=lstName, Path=SelectedValue}"  Height="23" Width="100" Margin="259,325,141,152">Add Qualification</Button>
        <Grid.CommandBindings x:Uid="key">
            <CommandBinding Command="Open" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/>
        </Grid.CommandBindings>
        <Grid.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
                               Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>                
            </Style>
            <DataTemplate x:Key="eble">
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                        <Setter TargetName="btnOpen" Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Grid.Resources>
        <Label Height="23" Width="100" Margin="17,35,383,442" RenderTransformOrigin="0.48,-3.217">Search</Label>
        <TextBox Name="txtSearch" Text="{Binding Qm.Search, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"  Height="23" Width="122" Margin="103,37,0,440" HorizontalAlignment="Left"></TextBox>
        <Button Name="btnGo"  Height="22" Width="30" Margin="205,37,265,441" RenderTransformOrigin="0.633,-1.318" Click="Button_Click_1">Go</Button>
        <ListBox Name="lstName"  ItemsSource="{Binding DocList}"  Width="218" Margin="17,82,265,152">
        </ListBox>
        <!--<Button Command="{Binding AddQual}" CommandParameter="{Binding ElementName=lstName, Path=SelectedValue}"  Height="23" Width="100" Margin="259,325,141,152">Add Qualification</Button>-->
    </Grid>
</UserControl>
4

1 回答 1

7

您可以为处理此问题的按钮创建样式。

    <Style x:Key="EnableButtonStyle" TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=yourListBox}" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False"></Setter>
            </DataTrigger>
         </Style.Triggers>
    </Style>

然后将其绑定到您的按钮:

<Button Style="{DynamicResource ResourceKey=EnableButtonStyle}"/>
于 2012-12-18T09:29:16.157 回答