wpf Textbox不会填写datatemplate

时间:2014-11-13 11:15:09

标签: wpf datatemplate

嗨,我有一个带有图像和文本框的递归数据模板,但是文本框没有填充整个宽度,这是在它进入数据模板之前所做的。我已经尝试将它放入dockpanel,宽度fillLastChild设置为true,但这没有任何区别。

        <DataTemplate x:Key="GroupQuestionTemplate">
        <Border BorderBrush="DarkSlateBlue" Margin="5">
            <Border.Style>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChild}" Value="True">
                            <Setter Property="Margin" Value="15,0,0,0"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <DockPanel Margin="10">
                <Button Content="Log" DockPanel.Dock="Top" HorizontalAlignment="Right"  Width="26" Height="21" Margin="-5,-5,-5,-5" Click="Button_Click"/>
                <DockPanel DockPanel.Dock="Left" HorizontalAlignment="Left">
                    <Image Name="CheckBox" Width="32" MouseDown="CheckBox_MouseDown" Margin="5,0,0,0">
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Setter Property="Source" Value="/Icons/checkbox48x48.png"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Answer.State}" Value="Done">
                                        <Setter Property="Source" Value="/Icons/check48x48.png"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Answer.State}" Value="NotDone">
                                        <Setter Property="Source" Value="/Icons/delete48x48.png"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Answer.State}" Value="NotApplicable">
                                        <Setter Property="Source" Value="/Icons/checkbox48x48.png"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Answer.State}" Value="NotAnswered">
                                        <Setter Property="Source" Value="/Icons/checkbox48x48.png"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </DockPanel>
                <StackPanel>
                    <TextBlock Text="{Binding Header}" Margin="10,0,0,0" TextWrapping="Wrap"/>
                    <TextBox Text="{Binding Answer.Comment, UpdateSourceTrigger=PropertyChanged}" Margin="10,0,0,0" TextWrapping="Wrap" AcceptsReturn="True" Height="50">
                        <TextBox.Style>
                            <Style TargetType="TextBox">
                                <Setter Property="IsEnabled" Value="False"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Answer.State}" Value="NotAnswered">
                                        <Setter Property="IsEnabled" Value="True"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Answer.State}" Value="NotApplicable">
                                        <Setter Property="IsEnabled" Value="True"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>

                    </TextBox>
                </StackPanel>
                <ItemsControl ItemsSource="{Binding Children}" ItemTemplate="{DynamicResource ResourceKey=GroupQuestionTemplate}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </DockPanel>
        </Border>
    </DataTemplate>
</Window.Resources>

这就是我使用它的地方

        <TabControl Grid.Row="1" ItemsSource="{Binding Groups}" TabStripPlacement="Left">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>

        <TabControl.ContentTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding Questions}" ItemTemplate="{StaticResource ResourceKey=GroupQuestionTemplate}">

                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>

            </DataTemplate>

        </TabControl.ContentTemplate>
    </TabControl>

2 个答案:

答案 0 :(得分:0)

尝试使用网格而不是StackPanel:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Text="{Binding Header}" Margin="10,0,0,0" TextWrapping="Wrap"/>
    <TextBox Grid.Column="1" Text="{Binding Answer.Comment, UpdateSourceTrigger=PropertyChanged}" Margin="10,0,0,0" TextWrapping="Wrap" AcceptsReturn="True" Height="50">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Answer.State}" Value="NotAnswered">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Answer.State}" Value="NotApplicable">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>

答案 1 :(得分:0)

我发现了问题。 我再次显示相同的datatemplate并且显然将文本框设置为文本框,因此文本框认为它不能更大。 我感谢大家帮忙