Changing a user control inside property outside it - wpf

I have a simple user control with a Ellipse and a TextBlock
I want to change the TextBlock.Text property inside the User Control from outside like this
I didnt find any simple explanation or way to do this
if there's any i would like to know
thanks

Expose it as a property:
public partial class MyUserControl : UserControl
{
public string Text
{
get { return MyTextBox.Text; }
set { MyTextBox.Text = value; }
}
}
If you want to set it via a binding, then you need to create a dependency property, but if you're just using literals and/or code-behind, then the above is fine.

Related

Defining new Visible property in UserControl. When set to false control disappears from designer

I created a UserControl1 : UserControl, where I defined VisibleNew property as
[Browsable(true)]
public bool VisibleNew
{
get
{
return Visible;
}
set
{
Visible = value;
}
}
So, if the control is placed on form, setting Visible = false does not hide it from the designer. But setting VisibleNew = false hides it!
What is wrong? How to make VisibleNew not to hide the control in designe time?
Tested on VS2010, VS2012
Controls have a designer, a class that runs at design time and make the control behave differently. The existing designer for a UserControl will intercept any assignments to the Visible property to prevent the control from getting invisible at design time. Problem is, it doesn't know beans about your VisibleNew property so cannot intercept it.
Technically you can create your own designer and have it intercept VisibleNew as well. It is however much simpler to build design-time awareness directly into your class. You can use the DesignMode property to detect that your control is being used at design time. Like this:
private bool visibleNew = true;
[Browsable(true), DefaultValue(true)]
public bool VisibleNew {
get {
return visibleNew;
}
set {
visibleNew = value;
if (!this.DesignMode) base.Visible = visibleNew;
}
}
Putting on my psychic debugging glasses, I don't think you actually want to do this at all. Tinkering with the Visible property is very tricky, it has quirky behavior at runtime. It will only return true if the control is actually visible to the user. I can only imagine you want to create your own property to work around that behavior. The correct way to do that looks like this:
private bool visibleIntent;
protected override void SetVisibleCore(bool value) {
visibleIntent = value;
base.SetVisibleCore(value);
}
[Browsable(false)]
public bool VisibleIntent {
get { return visibleIntent; }
}

Silverlight: How do i assign a usercontrol's property value to a textbox?

I have a user control which i have added to an outer form in silverlight. The user control has a textbox called txtRoleTitle, i have declared a property in the usercontrol's class called lableName and assigned txtRoleTitle.text to labelName as shown in the code below bellow,
in the silverlight property panel, under the miscellaneous menu, i have set labelName to "Landlord", then added another one of this user control to the outerform and set its labelName to Tenant. But this does not seem to work when i run the silverlight dialogue. The value of the labelName does not appear in the textbox during design and run time.
Here is my code below. thanks
public partial class UserRoleDetails : UserControl
{
public string labelName { get; set; }
public UserRoleDetails()
{
InitializeComponent();
this.txtRoleTitle.Text = labelName;
}
}
You are setting the value of txtRoleTitle.Text in your constructor, at the point of the assignment the labelName property will not have a value.
I think you need to look at making your labelName a dependency property and binding your txtRoleTitle control in the user control's xaml. Take a look at this example : http://stevenhollidge.blogspot.co.uk/2012/03/dependency-properties-in-user-control.html
First of all, your class should implement INotifyPropertyChanged interface
Secondly, you should make labelName property as notify property.
Thirdly, you should bind text property of txtRoleTitle to your notify property.
Alright, you are done.
Sample: http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/052a2b67-20fc-4f6a-84db-07c85ceb3303/
I had a look at dependancy property solution. Although while this may work if implemented properly, for what i am trying to do, this is an overkill solution. So i have a simple solution that works now, see code below:
public partial class UserRoleDetails : UserControl
{
public string labelName
{
get {return this.txtRoleTitle.Text;}
set {this.txtRoleTitle.Text = value;}
}
public UserRoleDetails()
{
InitializeComponent();
}
}

Updating a dependency property based on changes in the view model

I'm having some problems with data binding in WPF. Here's the scenario: I have made a user control which simulates a Dial Pad (i.e., an array of 12 buttons with the digits from '0' to '9' plus the '#' and 'Clear' keys). The control lives inside a class library and it's been implemented following the MVVM pattern, mainly because I need the components in the class library to be easily unit tested.
The view model for the control is quite simple, it basically updates a public "DialedNumber" string (which is internally connected to the model) every time the user presses a dial pad key button. The binding is working correctly and, by using the debugger, I can confirm that the "DialedNumber" variable inside the viewmodel is getting updated as I press button in the dial pad.
This DialPad control is used by a separate XAML file (Panel.xaml), which laids out several controls that belong to my custom class library.
Now, I'd like to add a TextBlock inside my Panel file in order to display the "DialedNumber" string held inside the DialPad. This is the code snippet in Panel.xaml:
<PanelControls:DialPad x:Name="MyDialPad" DialedNumber="55325"/>
<TextBlock Text="{Binding ElementName=MyDialPad, Path=DialedNumber}" />
The result I'm getting is that the textblock displays the correct number on start (i.e., "55325"), but its content doesn't get updated as I press the dial pad keys (even though the DialPad's viewmodel gets updated as I press new keys, as I've checked with the debugger).
Here's the code behind for the DialPad view:
public partial class DialPad : UserControl
{
public DialPad()
{
InitializeComponent();
DataContext = new DialPadViewModel();
}
public void DialedNumberChanged(object sender, EventArgs e)
{
return;
}
public DialPadViewModel DialPadViewModel
{
get { return DataContext as DialPadViewModel; }
}
public string DialedNumber
{
get
{
var dialPadViewModel = Resources["DialPadVM"] as DialPadViewModel;
return (dialPadViewModel != null) ? dialPadViewModel.DialedNumber : "";
}
set
{
var dialPadViewModel = Resources["DialPadVM"] as DialPadViewModel;
if (dialPadViewModel != null)
{
dialPadViewModel.DialedNumber = value;
}
}
}
}
Here's the DialPad view model:
public class DialPadViewModel : ObservableObject
{
public DialPadViewModel()
{
_dialPadModel = new DialPadModel();
}
#region Fields
private readonly DialPadModel _dialPadModel;
private ICommand _dialPadKeyPressed;
#endregion
#region Public Properties/Command
public DialPadModel DialPadModel
{
get { return _dialPadModel; }
}
public ICommand DialPadKeyPressedCommand
{
get
{
if (_dialPadKeyPressed == null)
{
_dialPadKeyPressed = new RelayCommand(DialPadKeyPressedCmd);
}
return _dialPadKeyPressed;
}
}
public string DialedNumber
{
get { return _dialPadModel.DialedNumber; }
set
{
_dialPadModel.DialedNumber = value;
RaisePropertyChanged("DialedNumber");
}
}
#endregion
#region Private Helpers
private void DialPadKeyPressedCmd(object parameter)
{
string keyPressedString = parameter.ToString();
if (keyPressedString.Length > 0)
{
if (char.IsDigit(keyPressedString[0]))
{
DialedNumber += keyPressedString[0].ToString(CultureInfo.InvariantCulture);
}
else if (keyPressedString == "C" || keyPressedString == "Clr" || keyPressedString == "Clear")
{
DialedNumber = "";
}
}
}
#endregion
}
Let me restate my problem: the textblock in Panel.xaml displays the correct number (55325) on start, but its value never gets updated as I press the DialPadButtons. I've placed a breakpoint inside DialPadKeyPressedCmd and I can confirm that the method gets executed everytime I press a key in the dial pad.
DependencyProperties are meant to point to some other property to get their value. So you can either point it to your DialPadViewModel.DialedNumber, or you can point it to some other string when the UserControl is used (either a binding or a hardcoded value like "551"), but you can't do both.
In your case, when someone binds to the DialedNumber dependency property, they are replacing the current value (the binding to DialPadViewModel.DialedNumber) with a new value.
Depending on how your code looks and what you want to do, there are a few ways around it.
First, you could insist that people who want to use your control also use your ViewModel, and don't make DialedNumber a public dependency property.
So instead of being allowed to create a custom class with a property of SomeOtherDialedNumber and binding
<DialPad DialedNumber="{Binding SomeOtherDialedNumber}">
they are forced to use the DialPadViewModel in their code anytime they want to use the DialPad control. For this to work, you would need to remove the this.DataContext = new DialPadViewModel in your code-behind the UserControl since the user will be providing the DialPadViewModel to your UserControl, and you can use an implicit DataTemplate to tell WPF to always draw DialPadViewModel with your DialPad UserControl.
<DataTemplate DataType="{x:Type DialPadViewModel}">
<local:DialPad />
</DataTemplate>
The other alternative I can think of is to synchronize your DependencyProperty with your ViewModel property with some PropertyChange notifications.
You would need to update DialPadViewModel.DialedNumber anytime the DialedNumber dependency property changes (You may need to use DependencyPropertyDescriptor.AddValueChanged for property change notification), and you would also have to write something to update the source of the DialedNumber dependency property anytime DialPadViewModel.DialedNumber changes.
Personally, if my UserControl has a ViewModel then I use the first option. If not, I get rid of the ViewModel entirely and build the logic for my UserControl in the code-behind, without a ViewModel.
The reason for this is that WPF works with two layers: a UI layer and a data layer. The DataContext is the data layer, and a ViewModel is typically part of the data layer. By setting the data layer (DataContext) explicitly in the UserControl's constructor, you are combining your data layer with your UI layer, which goes against one of the biggest reasons for using MVVM: separation of concerns. A UserControl should really just be a pretty shell only, and you should be able to place it on top of any data layer you want.
If you place your DialPad in your View, you can create a DialPadViewModel-Property (public+global) in your ViewViewModel:
public DialPadViewModel DialPadViewModel = new DialPadViewModel();
Now set the DataContext-Binding of your View to the ViewViewModel and bind the DialPads DataContext also to it, like
<local:DialPad DataContext="{Binding}"/>
Now you can bind to the properties in your DialPadViewModel:
<TextBox Text="{Binding Path=DialPadViewModel.DialedNumber}"/>
Thats how you can Access your DialPadViewModel from your View and your DialPad.
EDIT:
Now try changing your DialedNumber Property in your DialPad.xaml.cs like this:
public string DialedNumber
{
get
{
return DialPadViewModel.DialedNumber;
}
set
{
DialPadViewModel.DialedNumber = value;
}
}
EDIT 2: I found the Problem:
In your DialPad.xaml all your Commands were bound to the DialPadViewModel from the resources, while the TextBloc was bound to the DialPads DataContext, which is another instance of the DialPadViewModel.
So everytime you hit a DialPad-Button you changed the value of the DialedNumber from the resources' DPVM-instance not the DialedNumber from the DataContext's DPVM-instance.
It sounds like you can add a TextBox to your view and bind it's Text property to your view-model's DialedNumber property.
<TextBox Text="{Binding Path=DialedNumber}"></TextBox>
Your view-model property can look something like this:
private string _dialedNumber;
[DefaultValue("551")]
public string DialedNumber
{
get { return _dialedNumber; }
set
{
if (value == _dialedNumber)
return;
_dialedNumber= value;
_yourModel.DialedNumber= _dialedNumber;
this.RaisePropertyChanged("DialedNumber");
}
}
Let me know if I misunderstood your question.

Why WPF user control properties must be public to use them from XAML?

I'm working on a WPF custom control. The control has a property that is set in code behind and used in XAML. This property must be public for it work on XAML via a Binding. Why is this, if there is just one class?
<TextBlock Text="{Binding ElementName=PolicyBoxName, Path=FileNames[0]}" />
private string[] _fileNames;
public string[] FileNames
{
get
{
return _fileNames;
}
set
{
if (value != _fileNames)
{
_fileNames = value;
OnPropertyChanged("FileNames");
}
}
}
The XAML parsers constructs objects based on the supplied XML and sets their properties. It is no different from any other class, from a different namespace, that might wish to create your user control and set its properties. Without reflection, the constraints of the C# language demand that these properties are public for them to be set.

DependencyProperty Strangeness

I've put together a WPF application using ObservableCollection and Dependency Properties which is cool because I just have to add an item to the ObservableCollection and it shows up automatically, e.g. I display the objects in the collection as boxes on the screen in a wrappanel, each box showing its Title.
So then I wanted to have each item show not only its Title plus a prefix or suffix, but the Dependency Object property doesn't even seem to be used. I can put a break point on it and it is never reached.
Can anyone enlighten me why, if I add text to my outgoing property, that that text is never seen? I have read that the value is actually "stored not in the object but in WPF" but I don't understand what that means.
Why is the text this text will NOT be seen never output by the dependency object?
public class ApplicationItem : DependencyObject
{
public string Title
{
get
{
return (string)GetValue("this text will NOT be seen: " + TitleProperty);
}
set
{
SetValue(TitleProperty, "this text will be seen: " + value);
}
}
}
TitleProperty is not a normal property but a dependency property so if you want to retrieve the value of your TitleProperty you have to do :
var title = (string)GetValue(TitleProperty);
In WPF guideline, the public property to access a Dependency Property is not called by WPF and the binding engine (not necessary). This public property is only used by your code behind. So you MUST not add code logic inside your public property.
But you can use a FrameworkPropertyMetadata when you register your DP and provide a CoerceValueCallback to change the setted value.
You can also use a IValueConverter with your binding.
I got this to work for me:
public string Title
{
get
{
string value = (string)GetValue(TitleProperty);
return value + " postfix";
}
set
{
SetValue(TitleProperty, "Prefix " + value);
}
}
Is there a reason why you are attempting to modify the value when retrieving it rather than just modifying it when the value is set?

Resources