Note that there are some explanatory texts on larger screens.

plurals
  1. POChange DockPanel's background to match the disabled ListView's background color
    primarykey
    data
    text
    <p>I have a ListView inside a DockPanel. I need the ListView to be stretched horizontally and centered vertically inside the DockPanel. When the ListView is disabled (IsEnabled=false) I need to change the DockPanel's background to match the disabled ListView's background color. </p> <p>So, basically I want to avoid the situation that can be seen on the picture below.</p> <p><img src="https://i.stack.imgur.com/uiR3b.png" alt="enter image description here"></p> <pre><code>&lt;Window&gt; &lt;DockPanel IsEnabled="False"&gt; &lt;ListView IsEnabled="False" VerticalAlignment="Center" HorizontalContentAlignment="Center"&gt; &lt;ListViewItem Content="AAA"/&gt; &lt;ListViewItem Content="BBB"/&gt; &lt;ListViewItem Content="CCC"/&gt; &lt;/ListView&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre> <p>However, I don't want to explicitly declare colors anywhere in my code, because I don't know what kind of background color will a ListView use in different Windows environments (I'm not sure, but I guess different Windows theme/color settings can alter the default background color of the ListView - which potentially could be different from the color that I don't want to explicitly declare).</p> <p>Binding the ListView's background color to the DockPanel's background color doesn't work.</p> <p>So, right now I'm using the following workaround.</p> <pre><code>&lt;DockPanel.Style&gt; &lt;Style TargetType="{x:Type DockPanel}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding ElementName=parametersListView, Path=IsEnabled}" Value="False"&gt; &lt;Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/DockPanel.Style&gt; </code></pre> <p>I've extracted the resource key that my ListView's using in its ControlTemplate. On the template there is a Trigger declared on the IsEnabled property and it sets the control's background color to the color represented by the SystemColors.ControlBrushKey key.</p> <p>This seems to work, but I'm not sure if this is the right way to do this.</p> <p>Is there a way to do this in a more <em>robust</em> fashion, or this is the best that I could do?</p> <p>(On a side note: using SystemColors.ControlBrush instead of SystemColors.ControlBrushKey produces a different shade of grey, I'm not sure why.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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