This project has moved and is read-only. For the latest updates, please go here.

Checklistbox help

Oct 26, 2012 at 8:18 PM
Edited Oct 29, 2012 at 4:58 PM

Hi,

I am new to xaml wpf and mvvm.  we are using an extened checkboxlist which works well.

the problem i an having is setting the colurs for the checklistbox, by default it is whit background, all i want is to set this to another colour but have tried creating a theme file.

i have a checklistbosstyles.xaml file for the checkboxlist, but not sure how to layout the file so it does the theme, i tries <setter property="background" value="red"> but it only does the scrollbar area and not the white listbox background.

I have added the style file in the sttyles.xaml and does reference the style file.

In style.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="CheckListBoxStyles.xaml"/>       
    </ResourceDictionary.MergedDictionaries>
   <Style TargetType="xctk:CheckListBox" BasedOn="{StaticResource StandardUserControl}"/>   
</ResourceDictionary>

In view.xaml

        <Grid>
            <Grid.Resources>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Foreground="White" Text="Data Privileges" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBlock>
            <xctk:CheckListBox Name="checkListBoxData" ItemsSource="{Binding Path=DataPrivileges, Mode=TwoWay}"
                                    SelectedMemberPath="IsSelected"
                                  DisplayMemberPath="PrivilegeDescription" DockPanel.Dock="Top" Margin="0,22,0,15">
            </xctk:CheckListBox>

            <TextBlock Grid.Row="0" Grid.Column="1" Foreground="White" Text="System Privileges" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBlock>
            <xctk:CheckListBox Name="checkListBoxSystem"  ItemsSource="{Binding Path=SysPrivileges, Mode=TwoWay}"
                                    SelectedMemberPath="IsSelected"
                                  DisplayMemberPath="PrivilegeDescription" DockPanel.Dock="Top" Grid.Column="1" Margin="0,22,0,15">
            </xctk:CheckListBox>
        </Grid>

In the CheckListBoxStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                   
                    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PNC.Styles;component/Resources.xaml"/>       
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="StandardCheckListBox" TargetType="xctk:CheckListBox">
        <Setter Property="Background" Value="Red"/>  - doesnt work?
        <Setter Property="BorderBrush" Value="Transparent"/>       

    </Style>
</ResourceDictionary>

The problem here is i dont know how to create this file for a custom control like this.  I have other style files for normal controls such as datagrid, etc which work fine.

 

Any help here would be great as i am running out of time.

 

Thanks in advance

Nov 1, 2012 at 3:25 PM

You will have to set the style on your CheckListBox. Also, to change the background color of the CheckListbox, you have to change the background color of the SelectorItems which are the listBox items. Here's a simple example :

 <Window.Resources>
        <local:FileNames x:Key="myList" >
            <sys:String>Foo</sys:String>
            <sys:String>Bar</sys:String>
            <sys:String>Baz</sys:String>
        </local:FileNames>

        <Style x:Key="StandardSelectorItem" TargetType="xctk:SelectorItem">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
        </Style>
 </Window.Resources>
   
    <Grid>
            <xctk:CheckListBox Name="checkListBoxSystem"
                               ItemsSource="{StaticResource myList}"
                               ItemContainerStyle="{StaticResource StandardSelectorItem}" />
    </Grid>

Nov 2, 2012 at 2:36 PM

Wow, Thanks mate that did the trick, was stuck on this for so long.

Big thanks. :)