I'm new to Windows Phone 8 Development (You can say informed noob looking for answers) and I'm looking for help with the following problem:
I have usercontrol - more specifically a button containing Image and Text. I would like that button to have following properties:
- ContentImageNormal - that would be image that displays when the button is enabled
- ContentImageDisabled - That would be image that is dispalyed when the button is disabled
What i have now is usercontrol added in my project in folder UserControls, which I am able to use. I have created a style for it and changed the disabled button's backgeround etc.
What would i like to know:
- How to change the code-behind and other stuff needed so that I can use it as desired below?
(I will use the button six times in my program and i would like to use this usercontrol as a kind of template for it - to prepare it so I just specify ImageSources for both states and it will do the rest)
EXAMPLE OF DESIRED USAGE:
<UserControls:MainPageButton ContentText="Log In" ContentImageNormal="/ImagePathOrResourceETC.png" ContentImageDisabled="/ImagePathOrResourceETC.png"/>
XAML I have:
<UserControl x:Class="ProjectNameSpace.UserControls.MainPageButton">
<Grid x:Name="LayoutRoot">
<Button >
<Button.Content>
<Grid>
...omitted...
<Image x:Name="ContentImageHolder" ... />
<TextBlock x:Name="ContentTextBlock" .../>
</Grid>
</Button.Content>
</Button>
</Grid>
</UserControl>
C# code-behind I currently have:
...usings omitted...
public partial class MainPageButton : UserControl
{
...constructor and text setting omitted for brevity...
public ImageSource ContentImage
{
get
{
return ContentImageHolder.Source;
}
set
{
ContentImageHolder.Source = value;
}
}
}
}
It seem that what you are really trying to do is create a CustomControl not a UserControl. So me I will create a custom Control inheriting from Button instead, and use the button visual state to switch between the two image. Here is an implementation of that: Add this Style in ThemeS/Generic.xaml
and define the following class:
This is pretty trivial to whip up (well, it is for me, because i've got tons of snippets for most of the hard stuff), so here it is as a custom UserControl.
First, create your user control with your image. Yay.
extra hard, there.
Next, in the codebehind you have to do the following things. First, create DependencyProperties for each type of image representing whatever state you have. In my example, I'm omitting the InactiveImage and DerpImage properties for space reasons. You can see how the ActiveImage works. Next, you create a DependencyProperty for the state of the control. In the example I have a State enum that defines this, but you can do whatever you want. On change, I examine the new value and change the image as necessary. Easy.
And that's it. You would use it like this:
This assumes the DataContext is an instance with a property called
State
of typeThreeWay.State
. If not, a custom converter can be used to convert between whatever (nullable bool?) to the correct type.Jan,
For your problem, you are just missing one thing.
In your UserControl you have to create the DependencyProperties like this:
Then your property should be something like this:
Do this for each property you want to set...
See this example: http://stevenhollidge.blogspot.pt/2012/03/dependency-properties-in-user-control.html
I hope this helps you.
Regards, Sérgio Monteiro
You could have a simple binding to Image and to Text.
For your example:
Then you would create a Converter that would receive as value a True or False (enabled or not), and then return the image you want in each state.
Your converter could be something like
For the Text, if it is dependant on any property you could also do the same.