Reputation:
I have a C# class like this :
public partial class MyClass: UserControl
{
public String SomeText{get;set;}
...
public MyClass(String Text)
{
this.InitializeComponent();
SomeText = Text;
}
}
And I would like to get the attribute SomeText
in my XAML file. How to do something like this ?
<TextBlock Text="{THIS IS HERE I WANT TO HAVE SomeText}"></TextBlock>
I am new to C# so I don't know how to do. There must be a simple way?
Upvotes: 2
Views: 1563
Reputation: 44028
First of all, a correction:
that is a Property
(MSDN), not an Attribute
(MSDN), those are 2 completely different concepts.
Second:
WPF has a concept called DependencyProperties
(MSDN) which you must leverage in order to build complex custom UI controls in WPF.
Third:
There are times when you do not really need to declare properties in your UI. WPF empowers you to separate UI from data via its powerful DataBinding Engine and the MVVM Pattern. So, think again whether or not this control you are declaring is the right place to declare your string property.
I suggest you get familiar with all these concepts if you're going to work in WPF.
Give me more details about what you're trying to do and I can give you more insight.
Edit:
Basically what you need to do is to declare a DependencyProperty
in your control like this:
public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(string), typeof(mycontrol));
public string DisplayName
{
get { return GetValue(DisplayNameProperty).ToString(); }
set { SetValue(DisplayNameProperty, value); }
}
Then, in XAML:
<TextBlock Text="{Binding DisplayName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
Please keep in mind that WPF requires a completely different mindset from other frameworks, so my suggestion still stands: get familiar with MVVM and Databinding if you're planning to seriously work in WPF.
Upvotes: 1
Reputation: 2863
Give the UserControl
element a Name
and then bind to it as in the example below. If your text isn't going to change you should define it before your call to InitialiseComponent
.
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="root">
<Grid>
<TextBlock Text="{Binding SomeText,ElementName=root}"/>
</Grid>
</UserControl>
If your SomeText
is likely to change then you need to declare it as a DependencyProperty
instead of a plain old string
property. This would look something like below:
public string SomeText
{
get { return (string)GetValue(SomeTextProperty); }
set { SetValue(SomeTextProperty, value); }
}
// Using a DependencyProperty as the backing store for SomeText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SomeTextProperty =
DependencyProperty.Register("SomeText", typeof(string), typeof(UserControl1), new UIPropertyMetadata(""));
You should then change your binding to look like the below, which will cause the UI to update when you change the value of SomeString
.
<TextBlock Text="{Binding SomeText,ElementName=root,UpdateSourceTrigger=PropertyChanged}"/>
Upvotes: 3