我有一個DataTemplate和UniformGrid作爲ItemspanelTemplate的列表框,它顯示爲它應該除了一件事情,它忽略了我設置的背景顏色。所以我希望我的Listbox是透明的。這是我簡單的例子背後如何設置Listbox背景顏色,當它具有自定義datatemplate和ItemsPanelTemplate
public partial class MyListbox : UserControl
{
private List<int> foo = new List<int>();
public MyListbox()
{
InitializeComponent();
for (int i = 0; i < 52; i++)
{
foo.Add(i);
}
Listbox.ItemsSource = foo;
}
}
<Window xmlns:local="clr-namespace:ListboxBackground" Background="Red">
<local:MyListbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Window>
<UserControl x:Class="ListboxBackground.MyListbox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<DataTemplate x:Key="MyDataTemplate">
<Border Background="Green" Margin="2"/>
</DataTemplate>
</UserControl.Resources>
<ListBox x:Name="Listbox" ItemTemplate="{StaticResource MyDataTemplate}" IsEnabled="False" Background="Transparent">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="4" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Padding" Value="0" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</UserControl>
代碼,我設置背景顏色是透明的它忽略它沒關係。雖然我期待在這種情況下看到紅色背景色,因爲這是主窗口設置的內容。
任何想法爲什麼?我看着各種各樣的例子(在風格等設置,但沒有人會工作,背景永遠是白色)
謝謝
如果更改UserControl ListBox的'IsEnabled = true',它將起作用。有沒有任何理由將其設置爲false? – bars222
謝謝我沒有注意到這是它造成的。但有一個原因。在一個地方,我需要能夠通過點擊選擇位置(在表單上)來選擇一個位置。在另一個地方,這將成爲以前選擇的內容的代表(在瀏覽上),我不能再選擇。所以我試圖讓一個控件在兩種不同的場景中表現出很小的差異。但在這兩種情況下,背景都需要保持透明。 – adminSoftDK
我想這裏的答案http://stackoverflow.com/questions/2594714/change-background-color-of-disabled-listbox-in-windows-classic-theme – bars222