I created 3 state check box -
The basic idia is simple -
if the checkbox is in normal state => show image1
if in press state => show image2
if in mouse over state => show image3
.
I try to create 3 state button. This control will will change the image on each of those 3 state ( normal, pressed, mouseOver ). The images of each state will be define as a property from the c# code ==> that mean i will be able to re-use this code and change the properties of the image source. |
changinf images is done by binding the images to the code.
for some reason i dont see anything on the window when i using the control.
i dont see the images change, i dont see the control border and i dont know why.
attached the control code : because i realy dont know what wrong here ...
1. xaml:
<CheckBox x:Class="Controls.CheckBoxEx_" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Width="400" Height="400" d:DesignHeight="300" d:DesignWidth="300" > <CheckBox.Template> <ControlTemplate TargetType="{x:Type CheckBox}"> <Grid> <Image x:Name="Normal" Visibility="Visible" Source="{Binding NormalImp}" /> <Image x:Name="Pressed" Visibility="Hidden" Source="{Binding pressedImp}" /> <Image x:Name="MouseOver" Visibility="Hidden" Source="{Binding MouseOverImp}" /> Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="Pressed" Property="Visibility" Value="Hidden"/> <Setter TargetName="MouseOver" Property="Visibility" Value="Visible"/> Trigger> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/> <Setter TargetName="MouseOver" Property="Visibility" Value="Hidden"/> Trigger> <Trigger Property="IsChecked" Value="False"> <Setter TargetName="Normal" Property="Visibility" Value="Visible"/> <Setter TargetName="Pressed" Property="Visibility" Value="Hidden"/> <Setter TargetName="MouseOver" Property="Visibility" Value="Hidden"/> Trigger> ControlTemplate.Triggers> ControlTemplate> CheckBox.Template> CheckBox>
|
2. C# Code:
public partial class CheckBoxEx_ : CheckBox
{
public CheckBoxEx_()
{
InitializeComponent();
this.DataContext = this;
}
#region Field
public Image mouseOver;
public Image pressed;
public Image normal;
#endregion
#region Properties
public Image MouseOverImg
{
get
{
return mouseOver;
}
set
{
mouseOver = value;
}
}
public Image PressedImg
{
get
{
return pressed;
}
set
{
pressed = value;
}
}
public Image NormalImg
{
get
{
return normal;
}
set
{
normal = value;
}
}
#endregion
}
Suthish NairPosted Oct 13, 2010, 4:57 PM
YanshofPosted Oct 13, 2010, 4:01 AM
Thanks,
Its work fine.
But i really want to know ( and learn ) why my original code is not working fine ? What i did wrong ?
kaliappan RavikumarPosted Oct 13, 2010, 2:48 AM
Hi Friend
Add All the Three Images in ImageList
Now assign that ImageList to checkbox image List
Just Change Inageindex when u need
ImageList imglst = new ImageList();
imglst.Images.Add(global::ListBoxSample.Properties.Resources.Workflow);
imglst.Images.Add(global::ListBoxSample.Properties.Resources.Logo);
imglst.Images.Add(global::ListBoxSample.Properties.Resources.emulator);
checkBox1.ImageList = imglst;
checkBox1.ImageIndex = 0;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.ImageIndex = 2;
checkBox1.Text = " emulator";
}
}
private void checkBox1_MouseHover(object sender, EventArgs e)
{
checkBox1.ImageIndex = 1;
checkBox1.Text = " Logo";
}
private void checkBox1_MouseLeave(object sender, EventArgs e)
{
checkBox1.ImageIndex = 0;
checkBox1.Text = " Workflow";
}