bind dependency property from custom class to UI - wpf

have tried for almost a hole week to find how to bind my dependency property that is in a custom class (Elements) to a simple text box,
Text inside textbox must change every time when i send particular data, this is happening for the first time only,after that, textbox want be updated ??
i have tried every single example but i could not reach my goal, here is my code :
for the class where the depency object created :
public class Elements : DependencyObject
{
public static DependencyProperty TextDataProperty = DependencyProperty.Register
(
"TextData",
typeof(string),
typeof(Elements),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)
);
public string TextData
{
get
{
return (string)GetValue(TextDataProperty);
}
set
{
SetValue(TextDataProperty, value);
}
}
public void UpdateText(string sdata)
{
TextData = sdata;
}
}
i have refere to this class in xaml like so
xmlns:local="clr-namespace:WpfApplication1"
and to use it :
<Window.Resources>
<local:Elements x:Key="myElements" ></local:Elements>
</Window.Resources>
the text box that need to be updated is binded to the custom class like this :
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="12,61,0,0"
Name="textBox1"
VerticalAlignment="Top"
Width="237"
Text="{Binding TextData, Source={StaticResource myElements}}"
/>
what im doing wrong ?? can someone help please

From your comment: You are creating a new Elements object in
Elements _elements = new Elements();
_elements.UpdateText(textBox2.Text);
This is not the object that is used for the binding in
Text="{Binding TextData, Source={StaticResource myElements}}"
Change your code so that it accesses the object from the Resources:
var elements = (Elements)Resources["myElements"];
elements.UpdateText(textBox2.Text);
Then you should replace the TextBox by a TextBlock, because you only want to display text, but do not want the user to edit it.
You should also take a look at the MVVM design pattern and how it is used in WPF. There are plenty of online resources available.

Related

How to pass my view model to a user to main view model?

I made a main window that displays various user controls in a content control. In this window, I have the user controls and their accompanying view models in the XAML as DataTemplate Resources. This window has a button that needs to display the user control in the contentcontrol and instantiate the view model for it. How can i pass the resource to my RelayCommand, so that i can tell the command which user control and view model to use? I figured out how to pass a hard-coded string as the command parameter, but now I'm wanting to pass the x:Name so i can reuse this command etc for more than one View-ViewModel.
Main Window's XAML snippets:
<Window.Resources>
<!--User Controls and Accompanying View Models-->
<DataTemplate DataType="{x:Type EmployerSetupVM:EmployerSetupVM}" x:Key="EmployerSetup" x:Name="EmployerSetup">
<EmployerSetupView:EmployerSetupView />
</DataTemplate>
<DataTemplate DataType="{x:Type VendorSetupVM:VendorSetupVM}">
<VendorSetupView:VendorSetupView />
</DataTemplate>
</Window.Resources>
<Button Style="{StaticResource LinkButton}" Command="{Binding ShowCommand}" CommandParameter="{StaticResource EmployerSetup}">
...
In the Main Window's ViewModel, here relevant code so far:
public RelayCommand<DataTemplate> ShowCommand
{
get;
private set;
}
ShowCommand = new RelayCommand<string>((s) => ShowExecuted(s));
private void ShowExecuted(DataTemplate s)
{
var fred = (s.DataType); //how do i get the actual name here, i see it when i hover with intellisense, but i can't access it!
if (!PageViewModels.Contains(EmployerSetupVM))
{
EmployerSetupVM = new EmployerSetupVM();
PageViewModels.Add(EmployerSetupVM);
}
int i = PageViewModels.IndexOf(EmployerSetupVM);
ChangeViewModel(PageViewModels[i]);
}
...
in other words, how do i get the name of the my DataTemplate w/ x:Key="EmployerSetup" in the XAML? If it matters, I'm using MVVMLight too
Try using the Name property of the class Type:
private void ShowExecuted(DataTemplate s) {
var typeName = s.DataType as Type;
if (typeName == null)
return;
var className = typeName.Name; // className will be EmployerSetupVM or VendorSetupVM
...
}
I'd still say passing the DataTemplate to the VM just seems strange. I'd just have two commands and switch the command used in the Button.Style according to the conditions you got.
If you "have" to use a single RelayCommand or the world might end, I'd tend to use a static enum that you can reference from xaml for CommandParameter than pass the whole DataTemplate object.

How to change TextBlock Text from codebehind?

I have a custom control. There is a Stack Panel with Button and TextBlock in generic.xaml:
<StackPanel>
<TextBlock x:Name="StatusText" />
</StackPanel>
Then I have
public class MyClass : Control
{
// Constructor etc.
public static readonly DependencyProperty StatusTextProperty = DependencyProperty.Register("StatusText", typeof(TextBlock), typeof(MyClass), null);
public TextBlock StatusText
{
get { return (TextBlock)this.GetValue(StatusTextProperty); }
set { SetValue(StatusTextProperty, value); }
}
}
There is if with some logic in that happens after the button is clicked.
How do I change the Text property of TextBloc?
I thought that I can do something like this
StatusText.SetValue(TextBlock.TextProperty, "Some text here.");
But it always returns NullReferenceException (Object reference not set to an instance of an object.)
Should I use PropertyChangedCallback() on dependency property or what else do I need? I am missing something ;-)
You're taking the wrong approach - instead of trying to push the text into the text block from the control's class, you need the text block to pull the value from the control's class. The main steps you need to do are:
Change the type of the dependency property from TextBlock to string.
Bind the Text property of the TextBlock in your control template to the dependency property using a TemplateBinding binding expression. Something along the lines of:
<TextBlock Text="{TemplateBinding StatusText}" />
You can then simply set the text to be displayed to the property on your control.
Hope this helps...
Chris
You can type your question on google and find answer few times faster.

WPF - Dependency Properties Error

I'm working on a WPF project, and my intention is to make two specific RadioButtons alter properties of another specified Component. But for now, i'm just trying to store a String inside the RadioButton.
For that, I've created a behavior class:
public class AdjustBehavior : Behavior<RadioButton>
{
With this property:
public static DependencyProperty AdjustLabelContentProperty =
DependencyProperty.RegisterAttached("LabelContent", typeof(String), typeof(AdjustBehavior),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.Inherits));
And these getters and setters:
public static String GetLabelContent(RadioButton tb)
{
return (String)tb.GetValue(AdjustLabelContentProperty);
}
public static void SetLabelContent(RadioButton tb, String value)
{
tb.SetValue(AdjustLabelContentProperty, value);
}
On the XAML side, I did this:
<RadioButton Content="Banana" Height="16" HorizontalAlignment="Left" Margin="30,216,0,0" Name="radioButton1" VerticalAlignment="Top" GroupName="a" IsThreeState="False" IsChecked="True" Checked="radioButton1_Checked" >
<int:Interaction.Behaviors>
<i:AdjustBehavior LabelContent="Apple" />
</int:Interaction.Behaviors>
</RadioButton>
Where int: is the namespace to Interaction.Behaviors and i: is the namespace to the AdjustBehavior class. But whenever I start my application, LabelContent is set to null. Why?
I didn't post the rest of my Behavior class because I think it won't matter, but I'll do if necessary.
Thanks in Advance.
Clark
You should use DependencyProperty.Register, not RegisterAttached. This isn't being used as an attached property, but rather a standard dependency property.
Attached property requires target to be attached to. In your case that target is radio button,
so you should use
<RadioButton i:AdjustBehavior.LabelContent="Apple" ... />
If you need to just create property of AdjustBehavior, use normal dependency property, not attached.
LabelContent should be either an attached property on RadioButton or dependency property on AdjustBehavior .

Setting XAML property value to user control

I have a user control in WPF which i want the text of one of it's labels to be read from the XAML where it is used. Hence..
My User Control:
<UserControl x:Class="muc">
<Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">
<Label.Content>
<Binding ElementName="TestName" Path="." />
</Label.Content>
</Label>
</UserControl>
Then using it:
<mycontorls:muc TestName="This is a test" />
But it doesn't works ...
How can i read the properties ?
I tried the first two answers and what I got worked in code but not on XAML (also doesn't let you see changes in the design view when using the control).
To get a property working like any other native one, here is the full process:
(The sample adds a dependency property of type Nullable to show in the control as text or a default if null)
In the code file:
1.a Define a dependency property:
public static readonly DependencyProperty MyNumberProperty = DependencyProperty.Register("MyNumber", typeof(Nullable<int>), typeof(MyUserControl), new PropertyMetadata(null, new PropertyChangedCallback(OnMyNumberChanged)));
1.b Implement the OnMyNumberChanged Callback:
private static void OnMyNumberChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args){
// When the color changes, set the icon color PlayButton
MyUserControl muc = (MyUserControl)obj;
Nullable<int> value = (Nullable<int>)args.NewValue;
if (value != null)
{
muc.MyNumberTextBlock.Text = value.ToString();
}
else
{
muc.MyNumberTextBlock.Text = "N/A";
}
}
1.c implement the MyNumber property (not dependency) to use the dependency property for easy in code use:
public Nullable<int> MyNumber{
get
{
return (Nullable<int>)GetValue(MyNumberProperty);
}
set
{
SetValue(MyNumberProperty, value);
OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value)); // Old value irrelevant.
}
}
In the XAML file bind the TextBlock control's text to the property (not dependency) to get the default value of the dependency property in case it is not set by the user of the control (assuming you called your root element of the user control "RootElement"):
This code:
< TextBlock Name="MyNumberTextBlock" Text="{Binding MyNumber, ElementName=RootElement}"/>
If you give the root UserControl element a name, then you can refer to it using ElementName:
<UserControl x:Class="muc"
Name="rootElement">
<Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">
<Label.Content>
<Binding ElementName="rootElement" Path="TestName" />
</Label.Content>
</Label>
</UserControl>
You can also use the markup extension syntax to make it a little shorter:
<UserControl x:Class="muc"
Name="rootElement">
<Label Foreground="#FF7800" FontSize="20" FontWeight="Bold"
Content="{Binding TestName, ElementName=rootElement}"/>
</UserControl>
Also remember that your control will be created before its properties are set. You will either need to implement INotifyPropertyChanged or have TestName be a dependency property so that the binding is re-evaluated after the property is set.
I've only done this with Silverlight, but i wouldnt be surprised if it works in the exact same way!
// <summary>
// Xaml exposed TextExposedInXaml property.
// </summary>
public static readonly DependencyProperty TestNameProperty = DependencyProperty.Register("TestName", typeof(string), typeof(NameOfMyUserControl), new PropertyMetadata(string.empty));
// <summary>
// Gets or sets the control's text
// </summary>
public string TextExposedInXaml
{
get
{
return (string)GetValue(TestNameProperty );
}
set
{
SetValue(TestNameProperty , value);
// set the value of the control's text here...!
}
}
{Binding ElementName=x} binds to an element with name x in the element tree, there is nothing here that deals with property TestName. If you want a property on your user control, then you have to define the property in the class corresponding to that user control (in your case it would be muc), and use {Binding RelativeSource={RelativeSource FindAncestor, ...}} to reference it on your user control (see here for details), or give it a name so you can use ElementName.

WPF MVVM User Control binding issues

I have an application that uses MVVM. I have several items on the main window that bind to the ViewModel for that window. When I run it everything works. However, when I add a user control to the main window and try to bind to one of its dependency objects it throws an exception (“Object reference not set to an instance of an object”). The exception window just pops up on the screen and does not link to any particular place in code. And any other information in the exception is not helpful.
I’ve tried my best to trace this down but I’m not having any luck. In the constructor of window I’ve checked and verified that the item that it’s attempting to bind to exists and is an object (int[]). I’ve also manually set the property in the constructor with problems.
Here are some code snippets if anyone can notice anything.
Here is where I use the user control and attempt to bind to the 'view' property
<local:Histogram Grid.Row="2" Grid.ColumnSpan="2"
View="{Binding Path=HistogramData}"
Foreground="{DynamicResource FontColor}"
BucketStroke="{DynamicResource BucketStrokeBrush}"
BucketFill="{DynamicResource BucketFillBrush}"
SelectedBrush="{DynamicResource FamilyEditListViewSelectedBrush}"
DisabledForegroundBrush="{DynamicResource DisabledForegroundBrush}"
AxisBrush="{DynamicResource AxisBrush}"
MaxHeight="130" />
Here is the field in the view model that I am attempting to bind to:
public int[] HistogramData
{
get
{
return histogramData;
}
set
{
if (value != histogramData)
{
histogramData = value;
RaisePropertyChanged("HistogramData");
}
}
}
And in the constructor of the view model I instantiate the object
histogramData = new int[256];
And finally here is the view property in the user control
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register("View",
typeof(int[]),
typeof(Histogram),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ViewProperty_Changed)));
public int[] View
{
get { return (int[])GetValue(ViewProperty); }
set { SetValue(ViewProperty, value); }
}
I don't know if this is enough information to solve anything so if more code is req please let me know. I could also zip up the project if someone is so inclined to look at that. Thanks in advance.
You could try initialising the array when you initialise FrameworkPropertyMetaData on the dependency property.
new FrameworkPropertyMetadata(new int [256],
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ViewProperty_Changed))
I think that the program might be hitting a null reference exception before it manages to bind the dependency property to the viewmodel property.
Ok I've had a look at your example project and think i have a solution.
change the int[] in the viewmodel to a List<int>.
I'm not sure why this works. I hope there is no technical reason that list<int> is not suitable for you.
Here is what I have changed in the solution
in the viewmodel
public List<int> CustomData
{
get
{
return new List<int>(){0,1,2,3};
}
set
{
}
}
In the arraycontrol codebehind
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data",
typeof(List<int>),
typeof(ArrayControl),
new FrameworkPropertyMetadata(new List<int>()));
public List<int> Data
{
get { return (List<int>)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
In arraycontrol.xaml. Just added listbox to show data binding working
<UserControl x:Class="UserControlWithArray.Controls.ArrayControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TextBlock x:Name="MessageTextBlock" Text="ArrayControl"/>
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Data}"/>
</Grid>
</UserControl>
Use the debugger to get an exception stack trace. That should help you narrow the problem down.
There are several ways to do this. For example, you should be able to just view the details of the exception. Or you could open a watch window and enter the expression $exception and then hit evaluate.

Resources