样式动态创建的TextBoxes

时间:2014-01-07 14:37:21

标签: c# wpf xaml

我正在使用MVVM模式,这对我来说真的很新。我点击'添加标题',当文本框显示时,点击'添加问题'时也会发生同样的情况。错误的是它们恰好低于对方。当他们点击'添加标题'时,我希望文本框显示 '20'左侧的边距,然后点击'添加问题' 我希望保证金显示为 '40'。他们还需要在所有文本框之间留出 '20'的空格,因此不会直接位于文本框的下方。

XAML代码:

<Grid>

<Button Content="Add Question" Command="{Binding AddQuestionCommand}" Margin="615,19,179,724" />
    <Button Content="Add Title" Command="{Binding AddTitleCommand}" Margin="474,19,320,724" />

    <StackPanel>

        <ItemsControl ItemsSource="{Binding Title}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <ItemsControl ItemsSource="{Binding Question}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </StackPanel>


</Grid>

我尝试添加Padding属性,但它会使文本框的高度变大,我也尝试了Margin,但所有文本框都是动态创建的。

2 个答案:

答案 0 :(得分:1)

定义外观结构的好方法是使用Grid控件,指定行和列。 尝试这样的事情:

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ItemsControl Grid.Column="0"  ItemsSource="{Binding Title}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    <ItemsControl  Grid.Column="1" ItemsSource="{Binding Question}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

答案 1 :(得分:1)

试试这个

根据您的要求调整保证金

<Grid>
    <Button Content="Add Question" Command="{Binding AddQuestionCommand}" Margin="615,19,179,724" />
    <Button Content="Add Title" Command="{Binding AddTitleCommand}" Margin="474,19,320,724" />

    <StackPanel>

        <ItemsControl ItemsSource="{Binding Title}">
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="FrameworkElement.Margin" Value="20,20,0,0"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <ItemsControl ItemsSource="{Binding Question}">
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="FrameworkElement.Margin" Value="40,20,0,0"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding .}" Width="200" HorizontalAlignment="Left"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </StackPanel>


</Grid>