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

时间:2016-12-19 20:19:15

标签: c# wpf datatemplate

我是C#WPF编程的新手,我遇到了困难(花了一整天时间),想弄清楚如何创建一个系统:

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

2)groupbox有一个文本框(用于int输入)和一个组合框(在字符串中选择)。我需要使用列表的元素填充组合框。 我需要从文本框中获取输入并从组合框中选择并将其放在我的结构列表中。{String topic; int number}。

这需要为用户生成的许多组框完成。

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

XAML

<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集合,还有一些添加和删除项目的方法。

1 个答案:

答案 0 :(得分:0)

好的,根据我所看到的情况,我认为这是你可以解决的问题:

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}"/>

  3. 我认为以上应该有效。