I want to make a user Control in WPf with same properties and events like ListBox.(can add items , remove them , selecting ,...)
on way in windows App is use a user control which is inherit form ListBox. but in WPF I don't know how make User Control inherit from ListBox (or other WPF Control)!!!
I write this code but it had an exception
public partial class InboxListItem : ListBox
{
public InboxListItem()
{
InitializeComponent();
}
and It's Xaml file
<UserControl
x:Class="ListBoxControl.InboxListItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myTypes="clr-namespace:ListBoxControl"
/>
you cant make a UserControl inherit from ListBox. What you want is a CusomControl, and the xaml will traditionally live in Themes\Generic.xaml Keep in mind that you have to register the default style. Of course, you can just use the one provided by ListBox, if you want.
You should check this article, it provides some good information as well as links to more articles.
Related
What are the benefits and downsides of implementing a custom control in XAML by inheriting from UserControl:
<UserControl x:Class="MyButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button>
<!-- custom content here -->
<!-- custom behaviors in the code behind -->
</Button>
</UserControl>
vs inheriting from the control I'm putting inside the UserControl?
<Button x:Class="MyButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- custom content here -->
<!-- custom behaviors in the code behind -->
</Button>
There is no benefit to using a UserControl if you really want a Button. A UserControl just provides a simple way to create a control. From the UserControl Class page on MSDN:
If you do need to create a new control, the simplest way is to create a class that derives from UserControl. Before you do so, consider that your control will not support templates and therefore will not support complex customization. However, deriving from UserControl is a suitable model if you want to build your control by adding existing elements to it, similar to how you build an application, and if you do not need to support complex customization. (If you want to use templates with your control, derive from Control instead.)
As the remarks from MSDN note, using UserControl instead of Button as your base class will mean that your control cannot be templated, whereas when using a Button as the base class, you could still provide a new ControlTemplate.
You should always use the control that most closely suits your needs as the base class. The UserControl is only there to provide an easy way for us to add a collection of already existing controls to the UI. If that is not what you want to do, then don't use it.
I was searching for a 'waiting' animation for silverlight after I realized that GIF animations doesn't work in Silverlight. I found an excellent animation here. How can I use this in my application. Do I need to create any custom controls. I just want to place this over my dataview until the items gets populated.
Check out this implementation in Coproject on codeplex.
If you're using mvvm, you can wrap operations in a using. The BusyWatcher gets injected into the ViewModel using MEF.
[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public IBusyWatcher Busy { get; set; }
then:
using (Busy.GetTicket())
{
...
}
Create UserControl call it something sensible like WaitAnim1.
The sample you point to overuses Grids. The outer grid represent in your case the UserControl. Do the following to make your usercontrol from that original code:-
copy the xmlns:sys="clr-namespace:System;assembly=mscorlib" namespace to your UserControl element.
copy the whole Grid.Resources to directly under the <UserControl> tag and rename Grid.Resources to UserControl.Resources
copy whole <Grid x:Name="LayoutRoot" > element from the source code and replace the one in your usercontrol with it.
You now have a usercontrol that when displayed will show the animation.
I have a user control that has a textbox in it, and i am using a clear button on my main form to clear information from the entire main window. i would like to clear the textbox in the user control once the clear button is clicked as well. i havent found an easy way to do this. i have tried referencing the control's name in c# followed by a "." however the name of the text box does not show up. any help would be appreciated!
WPF declares controls in a UserControl as private. To make your TextBox public you declare it with a FieldModifier as in:
<TextBox x:FieldModifier="Public" />
where x is the xaml namespace xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml". However the recommended way of clearing a TextBox is to bind it to a property and then clear the property.
You should not try to directly access controls within a UserControl from external classes or code. The simple mechanism would be to add a Clear() method to the UserControl which clears all relevant controls and information inside the UserControl.
The textbox could be bound to the DataContext of the UserControl. So a way of clearing it might be setting the property that is bound to the Text property of the TextBox to an empty string.
I have a data driven Silverlight 4 business application with a fairly standard user interface. There's a side section that allows you to enter your search criteria, a standard Silverlight 4 datagrid control in another section that contains your search results (if any), and then a "details" section of the screen which shows the individual information of a single row of the grid when you click on it.
Just underneath my grid control, I have placed a Silverlight DataPager control. When my datagrid has databound search results, I want the DataPager control to be activated that lets you move forwards and backwards through the dataset.
I've got the whole user interface xaml page bound to a custom viewmodel class.
My viewmodel class has a public ObservableCollection property called "Applications". I then set the xaml of my datagrid control to bind to my Applications property:
{datagrid:DataGrid x:Name="grid1" ItemsSource="{Binding Applications}"}
The datagrid control binds to my viewmodel with no issues. However, I'm unable to find the correct xaml syntax to bind the DataPager control to point to my same viewmodel Applications property. So the end result is my DataPager control never activates and remains disabled.
I'm sure I'm missing something obvious, but hoping someone can send me a quick solution.
thanks in advance,
John
Turns out that the xaml for the
DataPager control needed to point to my datagrid control and the binding path to
ItemsSource:
I've created a simple UserControl in ExpressionBlend. The UserControl is a ractangle with a TextBlock in it. When i use this UserContol in a Silverlight project, i can not change the text in the textBlock of the control. Should give an acces to the TextBlock before using the Control?
HELP"_
Your user control should have public properties that map to its features. If you want the users of the control to be able to set the text, create a Text property. The implementation can be as simple as forwarding to the inner TextBox.
Exposing the inner control is not the right way to do it.