我嘗試使用 aListBox和來創建一個包含兩列的按鈕串列UniformGrid。在我遇到以下問題之前,一切似乎都很好。當我單擊它或將滑鼠懸停在它上面時,按鈕的邊距空間顯示為淺藍色。如何消除這種影響?

這是我的代碼:
<ListBox Width="1000" Grid.Row="1" VerticalAlignment="Top" VerticalContentAlignment="Top" Name="uniformGrid1" Margin="50" ItemsSource="{Binding SomeItemsList}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" Background="Transparent" Name="uniformGrid1"></UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="50" Height="70" Click="keyword_Click" Width="250"
Foreground="Black" FontSize="16" FontFamily="Helvetica Neue" FontWeight="Bold"
BorderBrush="SlateGray" Content="{Binding Name}">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0.073" />
<GradientStop Color="White" Offset="1" />
<GradientStop Color="#FFE9E9F9" Offset="0.571" />
<GradientStop Color="#FFD7D7EC" Offset="0.243" />
</LinearGradientBrush>
</Button.Background>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
uj5u.com熱心網友回復:
在每個專案ItemsSource被包裹在ListBoxItem里面ListBox。AListBox是派生自 的控制元件Selector,它是允許選擇專案的專案控制元件的基本型別。
表示允許用戶從其子元素中選擇專案的控制元件。
您指定的內容DataTemplate將ListBoxItem在運行時放置在 a中,它是內容的容器。此容器具有定義其外觀和視覺狀態的默認樣式和控制元件模板。您看到的是MouseOver狀態和Selected狀態。您可以通過更改此提取的默認樣式為ListBoxItem和調整,或通過撰寫自己的。
- 如何為控制元件創建模板 (WPF.NET)
但是,似乎您的意圖不同。您可能想要的只是UniformGrid根據系結集合在 a 中顯示按鈕,而不進行任何選擇。您可以通過使用ItemsControl替代來實作這一點。它不提供任何選擇功能,但允許您將集合與UniformGrid作為專案面板系結。
<ItemsControl Width="1000" Grid.Row="1" VerticalAlignment="Top" VerticalContentAlignment="Top" Margin="50" ItemsSource="{Binding SomeItemsList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" Background="Transparent" Name="uniformGrid1"></UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="50" Height="70" Click="keyword_Click" Width="250"
Foreground="Black" FontSize="16" FontFamily="Helvetica Neue" FontWeight="Bold"
BorderBrush="SlateGray" Content="{Binding Name}">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0.073" />
<GradientStop Color="White" Offset="1" />
<GradientStop Color="#FFE9E9F9" Offset="0.571" />
<GradientStop Color="#FFD7D7EC" Offset="0.243" />
</LinearGradientBrush>
</Button.Background>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
請注意,我洗掉了兩個中的一個,Name="uniformGrid1"因為重復的名稱會導致編譯錯誤。如果您的內容超出視口并且您需要滾動條,則必須添加一個ScrollViewer,因為它內置于 中ListBox,而不是內置于ItemsControl.
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<ItemsControl Width="1000" Grid.Row="1" VerticalAlignment="Top" VerticalContentAlignment="Top" Margin="50" ItemsSource="{Binding SomeItemsList}">
<!-- ...other code. -->
</ItemsControl>
</ScrollViewer>
uj5u.com熱心網友回復:
這里的問題是ListBox有一個選擇,并且選定的專案會突出顯示。您需要禁用此突出顯示以獲得所需的結果,例如按照本答案中ListBox.ItemContainerStyle所述進行設定。這將洗掉(淺藍色)選擇顏色。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372425.html
