2013-07-26 39 views
3

我可以通過代碼編輯ListBox包含器樣式而不是xaml嗎?編輯列表框容器樣式

因爲:

我有我自己的列表框的項目,我這樣做:

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

我需要的項目,以舒展的列表框的整個寬度。在我的計劃之前我用這個XAML:

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

但現在我只有C#和本地列表框

回答

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); 
}