【问题标题】:Styling Dynamically created TextBoxes样式化动态创建的文本框
【发布时间】:2014-01-07 23:00:11
【问题描述】:

我正在使用对我来说非常新的 MVVM 模式。我单击'添加标题',此时文本框显示,当我单击'添加问题' 时也会发生同样的情况。错误的地方在于它们正好显示在彼此下方。当他们点击 'Add Title' 我希望文本框显示在 '20' 左侧的边距,然后当我点击 'Add Question' 我希望边距显示为 '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 但所有文本框都是动态创建的。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案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>
    

    【讨论】:

    • 它说我的 ItemsSource 需要为空?
    • 请再次检查代码。我编辑了它。我错误地添加了额外的项目控制。
    • 我不知道那里有什么问题..您也可以像这样更改文本框的样式
    • A style intended for type 'TextBox' cannot be applied to type 'ContentPresenter'. 这是我添加 tragettype 时读取的错误内容。
    【解决方案2】:

    定义外观结构的一个好方法是使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多