Archive for the ‘XAML’ category

WPF: Bind Control Enabled to Checkbox Checked

July 23rd, 2009

This is a simple example of a nifty use of binding in WPF.

Say you have an element in your application such as a TextBox, ComboBox, or some RadioButtons that you want to keep disabled to the user unless the user checks a box. You may even want to disable a whole section of controls contained inside of a StackPanel or DockPanel.

Everything enabled

To accomplish this you bind the IsEnabled property of the target control (in this case a ComboBox) to the IsChecked property of the CheckBox.

XAML:

<StackPanel>
    <CheckBox x:Name="chkEnableBackup">Run Backup Sets</CheckBox>
 
    <StackPanel Orientation="Horizontal">
        <Label Margin="12,0,0,0">Run backup every</Label>
        <ComboBox Width="70" SelectedIndex="0"
          IsEnabled="{Binding ElementName=chkEnableBackup, Path=IsChecked}">
            <ComboBoxItem>Minute</ComboBoxItem>
            <ComboBoxItem>Hour</ComboBoxItem>
            <ComboBoxItem>Day</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</StackPanel>

That’s it! Our result is that when the box is not checked the control is disabled.

Control disabled!

WPF: Vista Blue Highlight Brush

April 17th, 2009

For the WPF application I’m currently working on for work I decided that I wanted to change the style for all ListBoxItems so that they looked a little more like list items in Vista when highlighted, instead of the boring white text on a black background.

So I found this wonderful guide to setting the style for IsSelected ListBoxItems across the application. You can find it here: http://www.uxpassion.com/2008/09/styling-wpf-listbox-highlight-color/

Then, for a nice light-blue, glossy vista look I used this gradient:

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStop Offset="0" Color="#FFE3F4FC"/>
        <GradientStop Offset="0.38" Color="#FFD8EFFC"/>
        <GradientStop Offset="0.38" Color="#FFBEE6FD"/>
        <GradientStop Offset="1" Color="#FFA6D9F4"/>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

It’s not perfect, but it looks pretty good. I also gave it a solid LightBlue border with a corner radius of 5. Here’s what the results looked like:
Vista Blue Highlight