Creating a custom WP7 control with extra XAML parameters - c#

I want to create a control that inherits from TextBox, which has an attribute called 'Filler'. I have the basic control created (named GhostBox), so I can load it in to a sample project and see it on the designer, but I don't know how to create custom XAML parameters (such as Text and Width which already exist). Please explain how to add these?
Example:
<ghostbox:GhostBox Name="ghostBox1" Text="" Height="72" HorizontalAlignment="Left" VerticalAlignment="Top" Width="460" Margin="0,80,0,0" Filler="Filler Text"/>
As you can see at the end of the tag, there is a Filler attribute. That's what I want to add, if I haven't made it clear enough.
Thanks.

They are called Dependency Properties. Have a read of this MSDN article about them.

Related

WPF create your own type

I want to create a template (resource dictionary) for my app. where my type inherits the button type and I then can call it through:
<my-custom-type inherit from button>
</my-custom-type inherit from button>
And of course in WPF.
More specifically, I would like to create copies of the control in the image below with simple XAML syntax as above.
There are two approaches to this, each with their own pros and cons:
Templates allow you to reuse a section of XAML. There is (almost always) no code-behind, and you certainly won't be deriving from Button. For example, if you wanted to have a bordered text box repeated in an ItemsControl:
<DataTemplate x:Key="MyDataTemplate">
<Border>
<TextBox/>
</Border>
</DataTemplate>
Or in a button class you use ContentTemplate:
<Button ContentTemplate={StaticResource MyTemplate}>
</Button>
And you would use it as XTemplate="{StaticResource MyDataTemplate}" in an existing control that used templates. This is usually the way to go. Note that the name of the property won't be Template, but ItemTemplate, or ContentTemplate or something similar.
The exception is if you want custom behavior, in which case you use a UserControl. This technically could inherit from Button though you usually wouldn't. Subclassing a basic control should only be done if you are sure you actually want to do that. Once your user control is created, the syntax would look similar to what you have in your question:
<local:MyButton>
</local:MyButton>
Note that "local" is a made-up xmlns. Your user control would consist of whatever controls you wanted, and you can expose "attributes" to the using code via dependency properties.

dynamically do changes in UI

I am new to WPF and C#. I need to create a Label and a Button which is in a Template.xaml. But the number of Label and Button creation is user specific which is another design.xaml. i.e if the user gives value 5, then five Label and Button needs to be created using the Template.xaml file.
I tried with something like below in design.xaml,
xmlns:local="clr-namespace:UserDesign"
<local:Template HorizontalAlignment="Left" VerticalAlignment="Top" />
<local:Template HorizontalAlignment="Left" VerticalAlignment="Top" />
<local:Template HorizontalAlignment="Left" VerticalAlignment="Top" />
But this seems to be a fixed one. I need to do this dynamically based on user input.
Without a more complete code example, it's hard to know exactly what your scenario is. But if what you're dealing with is a collection of objects, where each object corresponds to a single instance of some Template object (which presumably contains your Label and Button object), then you probably want to use WPF's data-templating features.
There is a reasonably good tutorial here: Data Templating Overview
The basic idea is to declare a DataTemplate object which is used for the ItemTemplate property of ItemsControl or a sub-class (e.g. ListBox). The DataTemplate will be declared to apply to a specific data type (e.g. the object type in your collection), and you will use bindings between the UI elements in the template and the data type to customize those elements for each data object.
Depending on what your Template itself is declared as, that might wind up being appropriate for use in a DataTemplate declaration. Or it might not…it's impossible to say without seeing an actual good, minimal, complete code example.

How to get an icon in stackPanel in a XAML file

I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.

Changing the order of properties when controls are added to a WPF Window

When adding new controls to a WPF Window (or other "custom" control), such as a label, the IDE prebuilds such as
<Label Content="Label" Grid.ColumnSpan="2" Grid.Row="6" Grid.RowSpan="2" Height="28" HorizontalAlignment="Left" Margin="54,11,0,0" Name="label1" VerticalAlignment="Top" />
I would like it to change the default order and formatting, such as...
<Label Name="label1"
Content="Label"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="?"
Grid.Column ="?"
Height="28"/>
Especially as a newbie to WPF, I hate how things are just "thrown" together by the IDE. I like to have the name of controls up front, primary alignment issues, then the where and extras regarding the control.
I Don't know the way of stablish a default order in the properties, but maybe Xaml Markup Styler can be of your interest. It's a plugin for VS that reformat your XAML (contextual menu) and sort the atributes based on their importance (The importance in the opinion of the plugin developer.
Anyway, I'm using this plugin right now and I recomend it.
http://xamlstyler.codeplex.com/
I know this is not exactly what you want, but maybe It's a good partial solution.
in the xaml, the order of the properties does not matter. the visual editor will generate them in the order in which it was coded to generate them. you can reorder them and remove the ones you want (default values will be used, if necessary), for the most part, to your hearts desire.
in your example (for example) you don't need Grid.Row or Grid.Column if your label is not a child of a Grid control. You can leave off the Height if you want to use the default Height. Really, the only thing you SHOULD probably set is the Content--but even that is optional.
now, the other thing you can do is add a <style> to your resources section. by using a <style> you can set all of the "defaults" to what you want. for example, you can set the background or text color to be the same on every <Label> or only on the ones you tell to use the style that you create.
here is a pretty decent article on styles and control templates.

Focus on Canvas overlapping the listbox in WP7

I have a situation here. I have a page containing a ListBox. The ListBox is populated with Items if it is able to fetch the data from a web service. Now when the user doesn't have network connectivity on his phone or the webservice doesn't respond back with Ok status, I want to show the user a pop-up with an option to Retry or select Ok to stay on the same page (though it sounds dumb). Now for this I used a Canvas:
<Canvas Name="Nonetwork" Height="150" Width="280" HorizontalAlignment="Center" VerticalAlignment="Center" Background="DodgerBlue" Visibility="Collapsed" Margin="111,160,92,160" >
<TextBlock VerticalAlignment="Top" Height="120" Width="280" Text="No Network is currently availabe" TextAlignment="Center" TextWrapping="Wrap" Foreground="White" FontSize="28" />
<Button Margin="30, 80" Height="60" Width="100" Content="OK" FontSize="18" Click="Ok_Click"/>
<Button Margin="150, 80" Height="60" Width="100" Content="Retry" FontSize="18" Click="Retry_Click"/>
</Canvas>
Well as most of you experienced guys would have guessed, the canvas is buried inside the listbox and is not accessible when there is no network connectivity. So I have a blank page with the canvas but the user is not able to click on Ok or Retry. Please help
Please do let me know if there is any other approach to solve this problem. I tried Popup but I cant Navigate to the main page from a pop-up since that is a user control page. Any help is higly appreciated
Well, I placed my Canvas below the ListBox and the problem was solved. I didn't know that positioning of the controls in the XAML would have so much effect ...
The order in which elements are rendered in Silverlight is determined firstly by where they appear in the visual object hierarchy and secondly by their ZIndex property.
The Canvas has a third attached property named ZIndex that you can use to override the default layering of elements. Although this Canvas.ZIndex attached property is defined by the Canvas class, it actually works with any type of panel.
You can also try Canvas.ZIndex property:
Canvas.ZIndex Attached Property
What you do is a wrong practice and not at all recommended.
ChildWindow is the class you should use to display such kind of dialog.
Using a Popup is also another approach you can use.
NOTE: I know the simplest approach would be to use MessageBox.Show(), but it would create a popup out of silverlight frame and does not allow theming/styling and other customizations.

Categories