Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically, after reviewing the google results, I started with the info from <a href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/5137aabc-bb3a-478a-9438-bc93dd9cc0ac/" rel="noreferrer">an MSDN discussion thread where Dr. WPF provided an answer</a>, which talks about styling a ListBox to look right. However, when the listbox is disabled, the background was an annoying color that I couldn't get rid of for the life of me, until I read <a href="http://msdn.microsoft.com/en-us/library/ms754242.aspx" rel="noreferrer">the MSDN example of the ListBox ControlTemplate</a>, which shows the secret Border element that was kicking my background butt.</p> <p>So, the final answer here was this style:</p> <pre><code> &lt;Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}"&gt; &lt;!-- ControlTemplate taken from MSDN http://msdn.microsoft.com/en-us/library/ms754242.aspx --&gt; &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt; &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt; &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/&gt; &lt;Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/&gt; &lt;Setter Property="ScrollViewer.CanContentScroll" Value="true"/&gt; &lt;Setter Property="MinWidth" Value="120"/&gt; &lt;Setter Property="MinHeight" Value="95"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBox"&gt; &lt;Border Name="Border" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" CornerRadius="2"&gt; &lt;ScrollViewer Margin="0" Focusable="false"&gt; &lt;StackPanel Margin="2" IsItemsHost="True" /&gt; &lt;/ScrollViewer&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter TargetName="Border" Property="Background" Value="Transparent" /&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="Transparent" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsGrouping" Value="true"&gt; &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ItemContainerStyle"&gt; &lt;Setter.Value&gt; &lt;Style TargetType="{x:Type ListBoxItem}" &gt; &lt;Setter Property="Margin" Value="2" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt; &lt;Border Name="theBorder" Background="Transparent"&gt; &lt;RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}"&gt; &lt;ContentPresenter /&gt; &lt;/RadioButton&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>Which provides a ControlTemplate for, and styles, the ListBox and the Items. And it gets used like this:</p> <pre><code> &lt;ListBox Grid.Column="1" Grid.Row="0" x:Name="TurnChargeBasedOnSelector" Background="Transparent" IsEnabled="{Binding Path=IsEditing}" Style="{StaticResource RadioButtonList}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainForm}}, Path=DataContext.RampTurnsBasedOnList}" DisplayMemberPath="Description" SelectedValuePath="RampTurnsBasedOnID" SelectedValue="{Binding Path=RampTurnsBasedOnID, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" /&gt; </code></pre> <p>The more I spend time with WPF, the more I think it makes the trivial insanely difficult and the insanely difficult trivial. Enjoy. -Scott</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload