0

我正在创建一个 Windows 8 应用程序,并且我需要在 TextBox 元素的 GotFocus 事件期间引发事件。我不确定出了什么问题,也不知道该朝哪个方向发展。 1. 我对 C# 中的事件一开始就不是那么好,2. 我认为它在 WindowsRT 中有点不同。TextBoxListArray() 方法通过另一个事件启动。

public sealed partial class MainPage : Page
{
    List<TextBox> textBox = new List<TextBox>();
    List<RichEditBox> editBox = new List<RichEditBox>();
    static int tally;
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void TextBoxListArray()
    {
        textBox.Add(new TextBox());
        int i = textBox.Count();
        i = i - 1;
        tally = i - 1;
        textBox[i].HorizontalAlignment = HorizontalAlignment.Stretch;
        textBox[i].VerticalAlignment = VerticalAlignment.Top;
        textBox[i].TextWrapping = TextWrapping.NoWrap;
        textBox[i].Margin = new Thickness(10);
        textBox[i].Text = i.ToString();
        textBox[i].IsReadOnly = true;
        textBox[i].Height = 40;
        stackNotes.Children.Add(textBox[i]);
        textBox[i].GotFocus += new EventHandler(TextBoxList_GotFocus);
    }

    private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
    {
        textBox[tally] = sender as TextBox;
        textBox[tally].Background = new SolidColorBrush(Colors.Yellow); 
    }
}
4

3 回答 3

3

这是right way您在 WPF(或任何基于 XAML 的 UI 框架)中寻找的内容

<Window x:Class="MiscSamples.TextBoxItemsControlSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBoxItemsControlSample" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}" x:Name="TextBox"/>
                <DataTemplate.Triggers>
                    <Trigger SourceName="TextBox" Property="IsKeyboardFocusWithin" Value="True">
                        <Setter TargetName="TextBox" Property="Background" Value="Yellow"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

代码背后:

public partial class TextBoxItemsControlSample : Window
{
    public TextBoxItemsControlSample()
    {
        InitializeComponent();

        DataContext = Enumerable.Range(0, 100).Select(x => "Text" + x.ToString());

    }
}

结果:

在此处输入图像描述

  • 没有single一行代码可以操作任何 UI 元素。你不需要那个。DataBinding足够强大,可以消除恐龙 UI 框架中丰富的所有可怕的 hack 的需要。
  • 对于任何给定的 TextBox,只要为 true,Trigger就会使 TextBox 的背景变为黄色。IsKeyboardFocusWithin这消除了对程序方法的需要。
  • 我没有在 WinRT XAML 中对此进行测试。可能存在差异(例如不同的属性名称等)。
  • ItemsControl您可以通过定义(例如)的属性来进一步自定义 UI ItemsPanel
  • WPF / XAML 摇滚。只需将我的代码复制并粘贴到 a 中File -> New Project -> WPF Application,然后自己查看结果。
于 2013-09-17T03:10:16.450 回答
0

您的代码的问题是tally用于从列表中获取焦点项目。因为您有sender参数,您可以将其转换为 TextBox,然后设置背景。

可能有更好的方法专门针对 WPF 作为其他答案。

private void TextBoxListArray()
{
    var tb =  new TextBox() { Text = textBox.Count(), ....... };
    tb.GotFocus += new EventHandler(TextBoxList_GotFocus);
    textBox.Add(tb);
    stackNotes.Children.Add(tb);
}

private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
{
     var txtBox = sender as TextBox;
     txtBox.Background = new SolidColorBrush(Colors.Yellow); 
}
于 2013-09-17T02:52:13.077 回答
0

这是我为使其工作所做的工作。这绝对是一个 hack,但它得到了我需要的结果。我还将通过 XAML 以“正确”的方式尝试它。

private void TextBoxListArray()
    {
        textBox.Add(new TextBox());
        int i = textBox.Count();
        i = i - 1;
        tally = i - 1;
        textBox[i].HorizontalAlignment = HorizontalAlignment.Stretch;
        textBox[i].VerticalAlignment = VerticalAlignment.Top;
        textBox[i].TextWrapping = TextWrapping.NoWrap;
        textBox[i].Margin = new Thickness(10);
        textBox[i].Text = i.ToString();
        textBox[i].IsReadOnly = true;
        textBox[i].Height = 40;
        stackNotes.Children.Add(textBox[i]);
        textBox[i].GotFocus += new RoutedEventHandler(TextBoxList_GotFocus);
        textBox[i].LostFocus += new RoutedEventHandler(TextBoxList_LostFocus);
    }

    private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBoxSender = sender as TextBox;
        textBoxSender.Background = new SolidColorBrush(Colors.Beige);
    }

    private void TextBoxList_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBoxSender = sender as TextBox;
        textBoxSender.Background = new SolidColorBrush(Colors.White);
    }
于 2013-09-17T03:17:02.713 回答