TargetName无法识别

时间:2012-12-18 09:21:23

标签: wpf xaml triggers

我尝试根据Button中的选择启用和停用ListBox。如果选择了某个值,则应启用该按钮 我正在尝试使用触发器,但它给了我一个错误

  

'targetname not recognized'。

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

1 个答案:

答案 0 :(得分:6)

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

    <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}"/>