简体   繁体   English

如何在代码中设置ListBox控件的Style属性

[英]How to set the Style property of a ListBox control in code

I have a part of Xaml code that I want to write in C# code. 我有一部分Xaml代码,我想用C#代码编写。 Code: 码:

<ListBox Name="listBox">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

I tried: 我试过了:

listBox.ItemContainerStyle = new Style();

Setter setter = new Setter();
setter.Property = ....??
setter.Value = true;

listBox.ItemContainerStyle.Setters.Add(setter);

But cant find the Focusable property to insert in setter. 但无法找到要在setter中插入的Focusable属性。 Can anyone help? 有人可以帮忙吗?

setter.Property = ListBoxItem.FocusVisualStyleProperty;

This link can help you. 这个链接可以帮到你。 It talks about creating templates in code behind. 它讨论了在代码背后创建模板。

This will work just fine. 这将工作得很好。

Style style = new Style(typeof(ListBoxItem));
style.Setters.Add(new Setter(ListBoxItem.FocusableProperty, false));

listBox.ItemContainerStyle = style;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM