繁体   English   中英

c#wpf从动态生成的DataTemplate对象中的文本框获取输入

[英]c# wpf getting input from textbox in dynamically generated DataTemplate objects

我是C#WPF编程的新手,并且在解决如何创建系统的过程中遇到了困难(花了整整一天的时间):

1)单击按钮即可生成UI元素的集合(在组框中)。

2)组框有一个文本框(用于int输入)和一个组合框(在字符串中选择)。 我需要用列表的元素填充组合框。 我需要从文本框中输入内容,从组合框中选择内容,然后将其放置在我的结构规则列表中。 int number}。

这需要针对用户生成的尽可能多的组框进行。

我的主要问题是,如何访问这些组合框以填充它们? 我如何从他们那里得到输入?

a

<Window x:Class="ExamMaker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExamMaker"
        mc:Ignorable="d"
        Title="Exam Preparation" Height="350" Width="525"
        Closing="Window_Closing">
    <Window.Resources>
        <DataTemplate x:Key="ruleTemplate">
            <GroupBox x:Name="Rulebox" Header="Rule " HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="56" Width="468">
                <Grid HorizontalAlignment = "Left" Height="36" Margin="0,0,-2,0" VerticalAlignment="Top" Width="458">
                    <Label x:Name="TopicRuleLabel" Content="Topic:" HorizontalAlignment="Left" Margin="5,4,0,0" VerticalAlignment="Top" Height="28"/>
                    <TextBox x:Name="NumberRuleTextBox" HorizontalAlignment="Left" Height="22" Margin="240,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="30" />
                    <ComboBox x:Name="RuleComboBox" HorizontalAlignment="Left" Margin="50,9,0,0" VerticalAlignment="Top" Width="120"/>
                    <Label x:Name="NumberRuleLabel" Content="Number:" HorizontalAlignment="Left" Margin="179,4,0,0" VerticalAlignment="Top" Height="28"/>
                </Grid>
            </GroupBox>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
            <TabItem Header="Exam Preparation">
                <Grid Background="#FFE5E5E5" Margin="0,0,0,0">
                    <ListView x:Name="listView" ItemsSource="{Binding Path=examQS}">
                        <ContentControl ContentTemplate="{StaticResource ruleTemplate}">

                        </ContentControl>
                    </ListView>
                </Grid>
            </TabItem>
            <TabItem Header="Question Form" Margin="-2,0,0,0" HorizontalAlignment="Left" Width="95">

            </TabItem>
            <TabItem Header="Setup" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="54">

            </TabItem>
        </TabControl>
    </Grid>
</Window>

至于c#,我那里只有一个Observable集合,还有一些从其中添加和删除项目的方法。

好的,根据我所看到的,我认为这是您可以执行的操作:

ItemsSource="{Binding Path=examQS}"

假设您具有ObservableCollectionexamQS属性。1.在examQs中定义一个属性,让我们说“ comboItemSource”

  1. 通过在ItemSource上添加绑定来更改DataTemplate中的这一行: <ComboBox x:Name="RuleComboBox" HorizontalAlignment="Left" Margin="50,9,0,0" VerticalAlignment="Top" Width="120" ItemSource="{Binding Path=comboItemSource} />

  2. 更改为使用ItemTemplate

    <ListView x:Name="listView" ItemsSource="{Binding Path=examQS}" ItemTemplate="{StaticResource ruleTemplate}"/>

我认为以上应该可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM