首页 > 解决方案 > 将 Label 的 target 属性设置为自定义 UserControl 中的对象

问题描述

只有标签和自定义控件的简单窗口。当Path=ValueContainer包含并使用访问键时,它会发出 Windows 通知声音,但焦点不会放在文本框中。使用 F12(转到定义)可以工作并导航到文本框。

如果Path=ValueContainer省略,则不会发出声音并且焦点不会改变。

<Window x:Class="DebuggingSearchBoxAccessKey.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DebuggingAccessKey"
        Title="MainWindow" Height="200" Width="200">
    <StackPanel Orientation="Horizontal">
        <Label Content="_Testing"
               Target="{Binding ElementName=searchBox, Path=ValueContainer}" />
        <local:SearchBox x:Name="searchBox" />
    </StackPanel>
</Window>

TextBox包含我希望访问密钥绑定到的自定义控件。

<UserControl x:Class="DebuggingAccessKey.SearchBox"
             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:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:DebuggingAccessKey"
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="100">
    <Grid>
        <TextBox x:Name="ValueContainer"
                 Text="Bazzle"/>
    </Grid>
</UserControl>

Text据我了解,我还将该属性实现为DependencyProperty.

public partial class SearchBox : UserControl
{
    public SearchBox()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextDependencyProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(SearchBox));

    public string Text
    {
        get => (string)GetValue(TextDependencyProperty);
        set => SetValue(TextDependencyProperty, value);
    }
}

绑定到标准文本框有效,如下所示。因此,我猜我错过了自定义控件所需的明显内容......

<StackPanel Orientation="Horizontal">
    <Label Content="_Foo" Target="{Binding ElementName=bar}" />
    <TextBox x:Name="bar" />
</StackPanel>

标签: c#wpf

解决方案


只能为 PROPERTIES 创建绑定。但是嵌套在 UserControl 中的 UI 元素是 FIELDS。

尝试添加属性。

XAML:

<UserControl x:Class="DebuggingAccessKey.SearchBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="100">
    <Grid>
        <TextBox x:Name="PART_ValueContainer"
                 Text="Bazzle"/>
    </Grid>
</UserControl>

代码背后:

using System.Windows;
using System.Windows.Controls;

namespace DebuggingAccessKey
{
    /// <summary>Interaction logic for SearchBox.xaml</summary>
    public partial class SearchBox : UserControl
    {
        public SearchBox()
        {
            InitializeComponent();
            ValueContainer = PART_ValueContainer;
        }

        public TextBox ValueContainer
        {
            get { return (TextBox)GetValue(ValueContainerProperty); }
            private set { SetValue(ValueContainerPropertyKey, value); }
        }

        // Use DependencyPropertyKey to create a read-only DependencyProperty ValueContainerProperty.
        private static readonly DependencyPropertyKey ValueContainerPropertyKey =
            DependencyProperty.RegisterReadOnly(nameof(ValueContainer), typeof(TextBox), typeof(SearchBox), new PropertyMetadata(null));


        // Using a DependencyProperty as the backing store for ValueContainer.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueContainerProperty = ValueContainerPropertyKey.DependencyProperty;

    }
}

推荐阅读