编辑ListBox容器样式

时间:2013-07-26 11:36:15

标签: c# silverlight windows-phone-7 windows-phone-8

我可以通过代码而不是xaml来编辑ListBox包含样式吗?

由于:

我有自己的ListBox项目,我这样做:

Expander.Expander a = new Expander.Expander();
                    a.IsExpanded = false;
                    TestItem TI = new TestItem();
                    TI.Tap += (s, e) =>
                    {
                        NavigationService.Navigate(new Uri((App.Current as App).Course, UriKind.Relative));
                    };
                    TI.Tag = "/CourseInfoPage.xaml";
                    ListBox list = new ListBox();
                    list.Items.Add(TI);
                    list.Height = 200;
                    a.Content = list;
                    a.HeaderContent = Semestris.Name;

                    Stack.Children.Add(a);

我需要在ListBox的整个宽度上拉伸项目。在我的程序之前我使用了这个xaml:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>

但现在我只有C#和原生ListBox

1 个答案:

答案 0 :(得分:0)

我认为,您可以设置代码后面的Style

private void Window_ContentRendered(object sender, EventArgs e)
{
    Style ListBoxItemStyle = new Style(typeof(ListBoxItem));
    ListBoxItemStyle.Setters.Add(new Setter(ListBoxItem.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
    Resources.Add(typeof(ListBoxItem), ListBoxItemStyle);
}