wpf 无法识别目标名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13929752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:39:39  来源:igfitidea点击:

TargetName not recognized

wpfxamltriggers

提问by Virus

I am trying to enable and disable a Buttonbased on selection in a ListBox. If a value is selected the button should be enabled.
I am trying to use triggers but it is giving me an error

我正在尝试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>

回答by Florian Gl

You could create a style for your button which handles this.

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

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

Then you bind it to your button:

然后你把它绑定到你的按钮上:

<Button Style="{DynamicResource ResourceKey=EnableButtonStyle}"/>