WPF"隐藏"小组背后

时间:2014-11-13 00:52:53

标签: c# wpf

我在项目中遇到了一个问题,我没有太多时间做研究。

这个项目的目标是允许在没有实际存在的情况下参加测试。

您会收到一个包含您的测试的文件,您会照看它们(时间有限)并发回包含您的答案的文件。

我在StackPanel中有一个TextBox,它本身包含在另一个StackPanel中。

所有控件都是以编程方式创建的。

正确添加控件但TextBox不会对鼠标输入做出反应....(实际上只有当文本框是ast项时,甚至只有最后一个像素)

UserControl XAML文件:

<UserControl x:Class="DataLibrary.View.Questions.ListQuestionInterface"
             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:localization ="clr-namespace:DataLibrary.Resources"
             xmlns:convert ="clr-namespace:DataLibrary.View.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Loaded="ListQuestionInterface_OnLoaded">
    <UserControl.Resources>
        <localization:LocalizedStrings x:Key="LocalizedStrings"/>
        <convert:getVisible x:Key="getVisible"/>
        <convert:getText x:Key="getText"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <StackPanel VerticalAlignment="Stretch" Orientation="Horizontal" x:Name="body" Grid.Row="0" FocusManager.IsFocusScope="True"/>
        <Label Grid.Row="0" Margin="0,10,0,0" x:Name="explanations"/>
        <Button Content="{Binding  Path=type, Converter={StaticResource getText}}"
                HorizontalAlignment="Right" 
                Margin="0,0,10,10" 
                VerticalAlignment="Bottom" 
                Grid.Row="1" 
                Width="120" 
                Height="20" 
                Click="DisplayAnswerButton_Click"
                Visibility="{Binding Path=type, Converter={StaticResource getVisible}}"/>
    </Grid>
</UserControl>

代码背后:

    public partial class ListQuestionInterface : UserControl
    {
        private UIElement _firstElement;
        ListQuestion q;
        private bool isTest;

        public questionType type
        {
            get
            {
                return q.Type;
            }
            set
            {
                Console.WriteLine("Attempted to write questionType");
            }
        }

        public ListQuestionInterface(ListQuestion question, bool isTest = true)
        {
            InitializeComponent();
            this.explanations.Content = question.Explanation;
            this.DataContext = this;
            this.q = question;
            this.isTest = isTest;
            refreshStackPanel();
        }

        private void refreshStackPanel()
        {
            bool first = true;
            this.body.Children.Clear();
            var enumerators = new Hashtable();
            foreach (Question subQuestion in this.q.SubQuestions)
            {
                enumerators.Add(subQuestion, subQuestion.interfaceEnumerator(isTest).GetEnumerator());
                ((IEnumerator)enumerators[subQuestion]).MoveNext();
            }
            //If the Alignemnt property has a value we'll want each pair of control to be aligned wit heach other
            //if not, we just want them stacked to the left
            if (q.Alignment.HasValue)
            {
                int maxCount = this.q.SubQuestions.Max(x => x.interfaceEnumerator(isTest).Count());
                for (int i = 0; i < maxCount; i++)
                {
                    var stack = new StackPanel
                    {
                        VerticalAlignment = VerticalAlignment.Stretch,
                        HorizontalAlignment = HorizontalAlignment.Center
                    };
                    foreach (Question subQuestion in this.q.SubQuestions)
                    {
                        try
                        {
                            var enumerator = (IEnumerator)enumerators[subQuestion];
                            var control = enumerator.Current as Control;
                            ((Panel)VisualTreeHelper.GetParent(control)).Children.Remove(control);
                            control.HorizontalAlignment = q.Alignment.Value;
                            Canvas canvas = null;
                            if (control.GetType() == typeof(Button) || control.GetType() == typeof(MaskedTextBox))
                            {
                                canvas = new Canvas();
                                if (control.GetType() == typeof(MaskedTextBox))
                                {
                                    var thick = control.Margin;
                                    thick.Left -= 5;
                                    control.Margin = thick;
                                }
                                if (first)
                                {
                                    this._firstElement = control;
                                    first = false;
                                }
                                control.Focusable = true;
                                canvas.Children.Add(control);

                            }

                            if (canvas == null)
                            {
                                stack.Children.Add(control);
                            }
                            else
                            {
                                stack.Children.Add(canvas);
                            }
                            enumerator.MoveNext();

                        }
                        catch
                        {
                            var blank = new Label
                            {
                                Content = "BLANK",
                                Visibility = Visibility.Hidden
                            };
                            stack.Children.Add(blank);
                            Console.WriteLine("No more items to display");
                        }
                    }
                    this.body.Children.Add(stack);
                }
            }
            else
            {
                var stack = new StackPanel
                {
                    VerticalAlignment = VerticalAlignment.Stretch,
                    HorizontalAlignment = HorizontalAlignment.Center,
                };
                foreach (var subQuestion in q.SubQuestions)
                {
                    var subStack = new StackPanel
                    {
                        VerticalAlignment = VerticalAlignment.Stretch,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Orientation = Orientation.Horizontal
                    };

                    var enumerator = subQuestion.interfaceEnumerator(isTest).GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        var control = enumerator.Current as Control;
                        control.HorizontalAlignment = HorizontalAlignment.Left;
                        if (control.GetType() == typeof(Button) || control.GetType() == typeof(MaskedTextBox))
                        {
                            if (first)
                            {
                                this._firstElement = control;
                                first = false;
                            }
                            control.Focusable = true;
                        }
                        ((Panel)VisualTreeHelper.GetParent(control)).Children.Remove(control);
                        subStack.Children.Add(control);
                    }
                    stack.Children.Add(subStack);

                }
                this.body.Children.Add(stack);
            }
        }

        private void DisplayAnswerButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (Question question in q.SubQuestions)
            {
                question.DisplayAnswers();
            }
            refreshStackPanel();
        }

        private void ListQuestionInterface_OnLoaded(object sender, RoutedEventArgs e)
        {
            this._firstElement.Focus();
        }
    }
}

我不认为该按钮与问题有关,因此我将保留转换器代码。

我已经检查了一些事情:

  • IsHitTestVisible永远不会设置为false
  • 定义其中一个堆叠面板,因为FocusScope不会更改任何内容
  • 如果我将所有控件放入画布并将画布放入堆栈面板,我可以自由点击我的控件,但它们的位置完全坏了。
  • 控件的实际宽度/高度足以与它们交互(60x20)

在使用以下trick在其他UserControls上设置第一个聚焦元素(不再在当前的VisualTree中)之后,似乎出现了问题

我真的需要帮助,因为我似乎找不到有类似问题的人。

这里有两个屏幕截图来说明问题:

No response

Got it !

黑色箭头显示我在拍摄截图之前点击的位置(顺便说一句,如果您知道任何可以使用鼠标进行屏幕截图的软件我正在使用它:))

1 个答案:

答案 0 :(得分:0)

好的,我的错 - -_-&#39;

我太累了,以至于我没有看到我的堆叠板实际上真的是在贴上标签

在我的代码中,我只有2个行定义,在第一个我把stackpanel和一个Label(占用了整个空间)。

因为它是后来宣布的,所以标签位于堆叠面板上方,从而阻止任何鼠标与其内容进行交互。

以下是更正后的XAML:

<UserControl x:Class="DataLibrary.View.Questions.ListQuestionInterface"
             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:localization ="clr-namespace:DataLibrary.Resources"
             xmlns:convert ="clr-namespace:DataLibrary.View.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Loaded="ListQuestionInterface_OnLoaded">
    <UserControl.Resources>
        <localization:LocalizedStrings x:Key="LocalizedStrings"/>
        <convert:getVisible x:Key="getVisible"/>
        <convert:getText x:Key="getText"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60*"/>
            <RowDefinition Height="20*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>
        <StackPanel VerticalAlignment="Stretch" Orientation="Horizontal" x:Name="body" Grid.Row="0" FocusManager.IsFocusScope="True"/>
        <Label Grid.Row="1" Margin="0,10,0,0" x:Name="explanations"/>
        <Button Content="{Binding  Path=type, Converter={StaticResource getText}}"
                HorizontalAlignment="Right" 
                Margin="0,0,10,10" 
                VerticalAlignment="Bottom" 
                Grid.Row="2" 
                Width="120" 
                Height="20" 
                Click="DisplayAnswerButton_Click"
                Visibility="{Binding Path=type, Converter={StaticResource getVisible}}"/>
    </Grid>
</UserControl>

事实上,控件确实隐藏在另一个-_-&#39;

之后

我现在知道这不是WPF的做事方式,但我不知道如何正确地将DataBind连接到模板

我仍然在XAML中绑定到DataTemplate的良好教程/起点上提出任何建议,我只能找到用于绑定单个值来控制属性的DataBinding