I've created a UserControl
with an ItemContext
DependencyProperty
. This property contains the name of the property of my DataContext
object the Control's text property should bind to.
I cannot figure out how to do this in XAML. I tried several steps, I'm quite near but couldn't find it.
Something like this:
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type me:UserControl}}, Path=ItemContext}" />
But here the contents of "ItemContext" is bound directly to the Text Property which I don't want. The contents of "ItemContext" let's say "Property1" is the name of the property in my DataContext I'd like to bind to.
In code it works like this:
this.txtValue0.SetBinding(TextBox.TextProperty, new Binding(this.ItemContext) { Mode =开发者_StackOverflow中文版 BindingMode.TwoWay });
Does someone have an idea?
Thanks
It sounds as though what you're trying to pass an external value into the Path
property of a Binding
object. That is, if ItemContext
's value is "Blob", you want to bind to DataContext.Blob
(not display the value "Blob").
That's easy to do in code, because you can reference the value directly (you pass this.ItemContext
to your binding as a one-time value). In markup, however, you can't do this. Instead, you're trying to bind a value to the Path
of the Binding
, but you can't (because it's not a DependencyProperty).
I'd suggest that a much easier solution would be to create a different property on your UserControl: instead of passing in "the name of the thing you want to bind to," why not just pass in the value of the thing?
I'm imagining that your current code looks like this:
<u:MyControl DataContext="{Binding SomeObject}" ItemContext="MyPropertyName" />
... and instead you should make it look like this:
<u:MyControl DataContext="{Binding SomeObject}" ItemValue="{Binding MyPropertyName}" />
... so the value is resolved externally of the control. Within the control, you can use @dowhilefor's solution to bind to the value.
Hope that makes sense!
If i understand it correctly. You want to use a dependency property from your usercontrol code behind, in your user control xaml?
Just give in xaml your user control a name x:Name="myUserControl"
and in your Binding write {Binding ElementName=myUserControl, Path=MyDependencyProperty}.
Thats at least how i do it, unless someone has a better idea of that strange limitation.
Try this one...
Xaml
<UserControl x:Class="WpfApplication1.DetailDataControl"
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" Name="root">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding ItemContext,ElementName=root}" Height="30" Width="100"></TextBlock>
</Grid>
</UserControl>
Code Behind
public partial class DetailDataControl : UserControl
{
public DetailDataControl()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemContextProperty = DependencyProperty.Register(
"ItemContext", typeof(string), typeof(DetailDataControl), new PropertyMetadata("default value"));
public string ItemContext
{
get { return (string)GetValue(ItemContextProperty); }
set { SetValue(ItemContextProperty, value); }
}
}
instead fo binding with relative source i have give name to user control. idon't some how relative source will not work as expected with self binding...
精彩评论