首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >选项卡-专注于自定义TextBox

选项卡-专注于自定义TextBox
EN

Stack Overflow用户
提问于 2015-07-21 11:44:19
回答 1查看 298关注 0票数 1

在我的应用程序中,我有一个TabControl。在一个TabItem上,有三个TextBoxes,我可以通过按Tab键在它们之间切换。

现在,我想将这个标准-TextBoxes替换为自定义-TextBoxes,它应该有一个空文本,如果文本是空的,该文本将被显示。

我的自定义-TextBox的XAML是:

代码语言:javascript
运行
复制
<UserControl x:Class="MyApplication.Controls.NullTextTextBox"
             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:converter="clr-namespace:ScM.Converter"
             mc:Ignorable="d" d:DesignHeight="24" d:DesignWidth="300"
             x:Name="nullTextTextBox" IsHitTestVisible="True" Focusable="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0" VerticalAlignment="Stretch" x:Name="tbInput" 
                 Text="{Binding ElementName=nullTextTextBox,Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 AcceptsReturn="{Binding ElementName=nullTextTextBox, Path=AcceptsReturn, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                 TextWrapping="{Binding ElementName=nullTextTextBox, Path=TextWrapping, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                 IsTabStop="True" />
        <TextBlock Grid.Column="0" VerticalAlignment="Top" Text="{Binding ElementName=nullTextTextBox,Path=NullText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"
                   FontStyle="Italic" Foreground="DarkGray" Margin="4,4,0,0" IsHitTestVisible="False"
                   Visibility="{Binding ElementName=nullTextTextBox, Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={converter:StringIsNullToVisibilityConverter}}"
                   Focusable="False"/>
        <TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock.Visibility>
                <MultiBinding Converter="{converter:DeleteButtonMultiConverter}">
                    <Binding ElementName="nullTextTextBox" Path="IsClearButtonVisible" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                    <Binding ElementName="nullTextTextBox" Path="Text" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBlock.Visibility>
            <Hyperlink TextDecorations="{x:Null}" Command="{Binding ElementName=nullTextTextBox, Path=ClearTextCommand, Mode=OneWay}"
                       Focusable="False" >
                <TextBlock FontFamily="Wingdings 2" Text="Î" Foreground="Red" FontWeight="Bold" FontSize="14" VerticalAlignment="Center" Margin="1,1,2,1"/>
            </Hyperlink>
        </TextBlock>
    </Grid>
</UserControl>

我想说,这个xaml的代码背后并不相关,因为只有注册的DependencyProperties

默认情况下-TextBox的行为与我所期望的一样。但是,如果我按Tab键,当焦点在一个NullTextBox内时,焦点将切换到TabHeader,而不是第二个NullTextBox。

NullTextBoxes所在的xaml如下所示:

代码语言:javascript
运行
复制
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <controls:NullTextTextBox Grid.Row="0" NullText="Value 1"/>
    <controls:NullTextTextBox Grid.Row="1" NullText="Value 2"/>
    <controls:NullTextTextBox Grid.Row="2" NullText="Value 3"/>
</Grid>

为什么当我按下Tab键时,我的第二和第三NullTextBox没有集中注意力?

我已经发现,如果我删除包含超链接的TextBlock,那么Tab订单就会像预期的那样工作。但我需要这个TextBlock..。

我的自定义文本框的代码隐藏看起来如下:

代码语言:javascript
运行
复制
public partial class NullTextTextBox : UserControl, INotifyPropertyChanged
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof (string), typeof (NullTextTextBox), new PropertyMetadata(default(string)));

    public static readonly DependencyProperty NullTextProperty = DependencyProperty.Register(
        "NullText", typeof (string), typeof (NullTextTextBox), new PropertyMetadata(default(string)));

    public static readonly DependencyProperty IsClearButtonVisibleProperty = DependencyProperty.Register(
        "IsClearButtonVisible", typeof (bool), typeof (NullTextTextBox), new PropertyMetadata(default(bool)));

    public static readonly DependencyProperty AcceptsReturnProperty = DependencyProperty.Register(
        "AcceptsReturn", typeof (bool), typeof (NullTextTextBox), new PropertyMetadata(default(bool)));

    public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
        "TextWrapping", typeof (TextWrapping), typeof (NullTextTextBox), new PropertyMetadata(default(TextWrapping)));

    public TextWrapping TextWrapping
    {
        get { return (TextWrapping) GetValue(TextWrappingProperty); }
        set
        {
            SetValue(TextWrappingProperty, value); 
            OnPropertyChanged();
        }
    }

    private ICommand clearTextCommand;

    public NullTextTextBox()
    {
        InitializeComponent();
        IsClearButtonVisible = false;
        Text = string.Empty;
        NullText = "Enter text here...";
        AcceptsReturn = false;
        TextWrapping = TextWrapping.NoWrap;
    }

    public bool AcceptsReturn
    {
        get { return (bool) GetValue(AcceptsReturnProperty); }
        set
        {
            SetValue(AcceptsReturnProperty, value);
            OnPropertyChanged();
        }
    }

    public ICommand ClearTextCommand
    {
        get { return clearTextCommand ?? (clearTextCommand = new RelayCommand<object>(p => ClearText())); }
    }

    public bool IsClearButtonVisible
    {
        get { return (bool) GetValue(IsClearButtonVisibleProperty); }
        set
        {
            SetValue(IsClearButtonVisibleProperty, value);
            OnPropertyChanged();
        }
    }

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
            OnPropertyChanged();
        }
    }

    public string NullText
    {
        get { return (string) GetValue(NullTextProperty); }
        set
        {
            SetValue(NullTextProperty, value);
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void ClearText()
    {
        Text = string.Empty;
        tbInput.Focus();
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

转换器使用:

代码语言:javascript
运行
复制
internal class DeleteButtonMultiConverter : MarkupExtension, IMultiValueConverter
{
    private static DeleteButtonMultiConverter converter;

    public DeleteButtonMultiConverter()
    {

    }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null && values.Length == 2 && values[0] is bool && values[1] is string)
        {
            if ((bool) values[0] && !string.IsNullOrEmpty((string) values[1]))
                return Visibility.Visible;
            return Visibility.Collapsed;
        }
        return values;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return converter ?? (converter = new DeleteButtonMultiConverter());
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-07-21 14:34:17

TextBlock超链接中更改为运行,如下所示(请注意,由于Run不支持VerticalAlignmentMargin,所以我已经删除或移动了这些属性):

代码语言:javascript
运行
复制
<TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="1,1,2,1">
    <TextBlock.Visibility>
        <MultiBinding Converter="{converter:DeleteButtonMultiConverter}">
            <Binding ElementName="nullTextTextBox" Path="IsClearButtonVisible" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
            <Binding ElementName="nullTextTextBox" Path="Text" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
        </MultiBinding>
    </TextBlock.Visibility>
    <Hyperlink TextDecorations="{x:Null}" Command="{Binding ElementName=nullTextTextBox, Path=ClearTextCommand, Mode=OneWay}"
               Focusable="False" >
        <Run FontFamily="Wingdings 2" Text="Î" Foreground="Red" FontWeight="Bold" FontSize="14" />
    </Hyperlink>
</TextBlock>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31538140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档