Silverlight UserControl with text field - silverlight

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.

Related

Issue : ListBox hides all the stack panel and text boxes with in them, Wpf

I want to bind my textboxes in wpf xaml. That is doing perfectly but when i get textbox with his x:Name="myTextbox" in codebehind. He says
"The name 'myTextbox' does not exist in the current context"
Using WPF, you cannot access items by name if there are inside a DataTemplate, like in your case, probably. However is not a good practice to modify the template using code behind. If you want to use correctly WPF, take a look at MVVM pattern.
In your case, the ListBox should be bound to an ObservableCollection and inside the template you can bind your textbox or whatever you have to the Item object.
Don't access the textbox like this : x:Name="myTextbox"
Instead access it like myTextbox.Text = "Hello World";.

WPF "Insert" control per binding

I have a UserControl without Content, because the control which should be shown inside of the UserControl is created at runtime. I would like to solve this like follows, but don't know how to implement it:
Create a Control-variable in the ViewModel
Set it at runtime when the content is created
Bind a content property (inside the UserControl) to that variable
The problem is, that I don't know how to bind to the control-variable.
Why just not to use ContentControl instead of UserControl and provide Content in runtime by introducing a DataTemplateSelector which able to provide right DataTemplate in runtime?
You can encapsulate your Content-area controls in DataTemplates and select appropriate one in runtime.

How to clear a TextBox in a User Control in WPF C#?

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.

Unable to databind a Silverlight4 DataPager control with MVVM

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:

User Control inherit from ListBox in Wpf?

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.

Resources