Silverlight:如何修改ListBox项的宽度以响应用户输入?

时间:2010-04-26 17:23:57

标签: silverlight listboxitem

我有一个简单的Silverlight 3 UserControl,其宽度根据用户输入增加或减少。

除ListBox项目外,控件会根据需要变得更宽或更窄。无论Horizo​​ntalContentAlignment设置为'Stretch',ListBox项都会水平增长以适合其内容。

我是否可以在ListBox.ItemContainerStyle上设置属性以告诉它使用父ListBox扩展/缩小?此列表框中不需要水平滚动。

或者有没有办法指定可以在运行时修改的ItemTemplate的StackPanel宽度?我已经将它绑定到StaticResource,但不明白我是否应该能够更改资源值。我可以创建并绑定到UserControl本身的DependencyProperty吗?我还没有在xaml中确定这个语法。

代码:

<UserControl x:Class="TheAssembly.GraphicViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:userControls="clr-namespace:TheAssembly"
xmlns:core="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
    <userControls:DictionaryAttributeConverter x:Name="MyDictionaryAttributeConverter" />
    <core:Double x:Key="ListItemWidth">155</core:Double>       
</UserControl.Resources> 
<Grid x:Name="LayoutRoot"  Width="175" >
    <Border Style="{StaticResource DraggableWindowBorder}">
        <StackPanel x:Name="RootStackPanel" Orientation="Vertical" HorizontalAlignment="Stretch">
            <Border Background="Black" HorizontalAlignment="Stretch" Margin="0">  
                <TextBlock x:Name="Header" Foreground="White" FontSize="14" TextWrapping="Wrap" Margin="2,0,2,0" 
                           Height="25" HorizontalAlignment="Left"
                           Text="{Binding HeaderText}"/>
            </Border>
            <TextBlock x:Name="Title"  Style="{StaticResource GraphicViewerDetail}" FontSize="12" FontWeight="Medium" TextWrapping="Wrap" 
               Text="{Binding Title}" Margin="3,0,0,0" HorizontalAlignment="Left"/>
            <ListBox x:Name="AttributeListBox" ItemsSource="{Binding Attributes}" BorderBrush="Red" HorizontalContentAlignment="Stretch"
                 Foreground="AntiqueWhite" Background="Transparent" IsEnabled="False" HorizontalAlignment="Stretch"  
                     ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="Margin" Value="0,-2,0,0"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="ListBoxItemStackPanel" HorizontalAlignment="Stretch" Orientation="Vertical" >
                            <TextBlock FontSize="10" Text="{Binding Key}" Foreground="White" FontWeight="Bold"  HorizontalAlignment="Stretch"
                                  Margin="2,0,0,0" TextWrapping="Wrap"/>
                            <TextBlock FontSize="10"  Text="{Binding Value}" Foreground="White" Margin="6,-2,0,0" TextWrapping="Wrap" 
                                       HorizontalAlignment="Stretch" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Border>
</Grid>

0 个答案:

没有答案