[UWP]如何限制清除按钮以清除UWP中TextBox中的值? [英] [UWP]How to restrict the clear button to clear the values in the TextBox in UWP?

查看:151
本文介绍了[UWP]如何限制清除按钮以清除UWP中TextBox中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,

我想限制清除按钮以根据特定条件清除TextBox中的值。例如,如果TextBox的文本是"MSDN",则该文本的文本是"MSDN"。然后当我点击清除按钮("X")时,该值不应该清楚。我知道这个清除按钮没有
没有直接事件。所以我通过VisualTreeHelper取了这个按钮并调用了它的click事件。谁能告诉我是否有办法实现这一目标?

I want to restrict the clear button to clear the values in TextBox based on specific conditions. For example, if text of the TextBox is "MSDN" then when I click the clear button("X") the value should not be clear. I know that there is no direct event for this clear button. So I have taken this button through VisualTreeHelper and called it's click event. Can anyone tell me whether there is any way to achieve this?

问候,

Keerthana

Keerthana

推荐答案

嗨  Keerthana Jegannathan,

Hi Keerthana Jegannathan,

似乎删除文本框中的按钮不是很好的做法。

It seems to remove the Button in Textbox is not a good practice.

如果您想通过VisyalTreeHelper获取按钮,请参阅  VisualTreeHelper类。我们可以在TextChanged事件中获取按钮,我们
可以设置 可见性为折叠。

If you want to get the Button by the VisyalTreeHelper, please refer the VisualTreeHelper class. We can get the Button in the TextChanged event, that we can set the Visibility to Collapsed.

例如:

internal static void FindChildren<T>(List<T> results, DependencyObject startNode) where T : DependencyObject
{
    int count = VisualTreeHelper.GetChildrenCount(startNode);
    for (int i = 0; i < count; i++)
    {
        DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
        if ((current.GetType()).Equals(typeof(T)) || (current.GetType().GetTypeInfo().IsSubclassOf(typeof(T))))
        {
            T asType = (T)current;
            results.Add(asType);
        }
        FindChildren<T>(results, current);
    }
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    List<Button> buttons = new List<Button>();
    FindChildren<Button>(buttons, MyText);
    buttons[0].Visibility = Visibility.Collapsed;
}

最好的问候,

Jayden Gu

Jayden Gu


这篇关于[UWP]如何限制清除按钮以清除UWP中TextBox中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆