2016-02-10 37 views
0

我有一個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> 

代碼,我設置背景顏色是透明的它忽略它沒關係。雖然我期待在這種情況下看到紅色背景色,因爲這是主窗口設置的內容。

任何想法爲什麼?我看着各種各樣的例子(在風格等設置,但沒有人會工作,背景永遠是白色)

謝謝

+0

如果更改UserControl ListBox的'IsEnabled = true',它將起作用。有沒有任何理由將其設置爲false? – bars222

+0

謝謝我沒有注意到這是它造成的。但有一個原因。在一個地方,我需要能夠通過點擊選擇位置(在表單上)來選擇一個位置。在另一個地方,這將成爲以前選擇的內容的代表(在瀏覽上),我不能再選擇。所以我試圖讓一個控件在兩種不同的場景中表現出很小的差異。但在這兩種情況下,背景都需要保持透明。 – adminSoftDK

+0

我想這裏的答案http://stackoverflow.com/questions/2594714/change-background-color-of-disabled-listbox-in-windows-classic-theme – bars222

回答

0

我無法重現你的行爲。一切正常。我已經編輯:

  • 我只是在電網的背景增加了一個形象:
  • 添加爲DataTemplate「MyDataTemplate」
  • 刪除IsEnabled財產TextBlock

讓我們來看看完整的代碼:

<Window.Resources> 
    <DataTemplate x:Key="MyDataTemplate"> 
     <Border Background="Green" Margin="2"> 
      <TextBlock Text="1"/> 
     </Border> 
    </DataTemplate> 
</Window.Resources> 
<Grid > 
    <Grid.Background> 
     <ImageBrush ImageSource="disco-lightball.jpg"/> 
    </Grid.Background> 
    <StackPanel> 
     <ListBox x:Name="Listbox" ItemTemplate="{StaticResource MyDataTemplate}" 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> 
    </StackPanel> 
</Grid> 

我們可以看到的是:

enter image description here

+0

謝謝你的答案,但我需要啓用設置如果可能的話,在某些情況下爲假,請閱讀我的問題下面的評論。 – adminSoftDK

1

根據我的意見。您可以在UserControl之內更改ListboxIsEnabled = true或將其添加到此(如果Listbox應始終透明)。

<ListBox.Style> 
    <Style TargetType="ListBox"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> 
      <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Transparent"/> 
     </Style.Resources> 
    </Style> 
</ListBox.Style> 
+0

我剛剛測試過它,它不工作,如果IsEnabled = false,背景仍然是白色:( – adminSoftDK

+0

也許它發生的原因使用不標準或aero窗口主題。我已經在標準的Windows主題測試。找到系統顏色,對禁用的列表框背景做出響應 – bars222

+0

如果你改變了'Listbox'的'Template',那麼它就不起作用了,在這種情況下,你可以在模板中添加Trigger for IsEnabled = false' – bars222