带有ElementName的WPF绑定异常

时间:2017-02-20 14:13:48

标签: c# wpf xaml

我有一个简单的窗口,我希望在打开TextBox时设置焦点。我尝试使用XAML中的FocusManager.FocusedElement但在运行时在Visual Studio的调试输出窗口中出现以下错误。

  

System.Windows.Data错误:4:无法找到引用'ElementName = TxtBoxAttributeValue'的绑定源。 BindingExpression :(没有路径);的DataItem = NULL; target元素是'NewAttributeView'(Name =''); target属性是'FocusedElement'(类型'IInputElement')

这是XAML代码。

<metro:MetroWindow x:Class="Figeas.Configuration.Editor.View.NewAttributeView"
      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" 
      xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:viewModels="clr-namespace:Figeas.Configuration.Editor.ViewModel"
      mc:Ignorable="d"
       BorderThickness="1" BorderBrush="Black"        
        SizeToContent="WidthAndHeight"
        ShowMinButton="False" ShowMaxRestoreButton="False"
        FocusManager.FocusedElement="{Binding ElementName=TxtBoxAttributeValue}" 
        WindowStartupLocation="CenterOwner"           
      Title="New Attribute">
    <metro:MetroWindow.DataContext>
        <viewModels:NewAttributeViewModel/>
    </metro:MetroWindow.DataContext>
    <Border BorderThickness="10">
        <StackPanel Orientation="Vertical">
            <WrapPanel>
                <Label Content="Attribute Name:"/>
                <Label Style="{StaticResource LblValue}" Content="{Binding AttributeName, Mode=OneWay}"/>
            </WrapPanel>
            <WrapPanel >
                <Label Content="Attribute Value:"/>
                <TextBox x:Name="TextBoxAttributeValue" Text="{Binding AttributeValue}"/>
            </WrapPanel>
            <Button Content="Save" Width="100" Click="BtnSave_Click"  IsDefault="True"/>
        </StackPanel>
    </Border>
</metro:MetroWindow>

我假设嵌入在堆叠面板中,TextBox无法使用其名称直接引用,但我不确定如何修复此问题。

0 个答案:

没有答案