Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置动态创建的文本框的样式_C#_Wpf_Xaml - Fatal编程技术网

C# 设置动态创建的文本框的样式

C# 设置动态创建的文本框的样式,c#,wpf,xaml,C#,Wpf,Xaml,我正在使用MVVM模式,这对我来说真的很新。我单击“添加标题”,此时文本框显示,单击“添加问题”时也会出现同样的情况。错误的是,它们正好出现在彼此的下方。当他们单击“添加标题”时,我希望文本框显示在'20'左侧的空白处,然后当我单击“添加问题”时,我希望空白处显示为'40'。它们还需要在所有文本框之间留出'20'的空间,这样就不会直接在文本框下方 XAML代码: <Grid> <Button Content="Add Question" Command="{Binding A

我正在使用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
,但所有文本框都是动态创建的。

定义外观结构的一个好方法是使用网格控件,指定行和列。 试着这样做:

 <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>

试试这个

根据您的要求调整保证金

<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>


它说我的ItemsSource需要为空?请再次检查代码..我编辑了它。我错误地添加了额外的itemscontrol。我不知道那里有什么问题..您可以更改textbox的样式,也可以像这样
用于“textbox”类型的样式无法应用于键入“ContentPresenter”。
这是我添加tragettype时错误显示的内容。