views:

1146

answers:

5

I've create user control like this:

public partial class View
    {
        public View()
        {
            InitializeComponent();
        }

        public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(TeaserView) );

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

    }

XAML:

<UserControl x:Class="Controls.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="200" Width="164">
    <Grid VerticalAlignment="Stretch"
          x:Name="Preview">

        <Label Height="28"  Content="{Binding ElementName=Preview, Path=Name}" Background="LightYellow" x:Name="name" VerticalAlignment="Top" ></Label>
    </Grid>
</UserControl>

and use it in Window1 simply in XAML:

<controls:View Height="200" Name="View1" Width="164" />

and I try set the Content in C# (Name property in this sample) but it does'n work, label's content is still empty. (All refereces, etc. are good) What's wrong?

A: 

Have you set the datacontext on the user control? Try setting it to point to its own codebehind:

DataContext="{Binding RelativeSource={RelativeSource Self}}"
Guy Starbuck
A: 

Copied your exact code and it works fine.

However, it's not doing what you're probably expecting it to do. You're setting the source of the binding to the Grid instance. Therefore, the Name property will yield "Preview". The Name property you've defined in your UserControl is ignored because there's already a Name property on UserControl.

HTH, Kent

Kent Boogaart
A: 

Hi Kamilos.

Your code is wrong. You bind to Grid.Name property, which is "Preview", not to View.Name.

I really encourage you to go read from A to Z "DataBinding Overview" on MSDN. It worth your time, trust me :). In fact whole "Windows Presentation Foundation" section would be worth your attention.

As for your code, the following will work:

<UserControl x:Class="WpfApplication5.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="300"
             Width="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <Grid>
    <Label Height="28"
           Content="{Binding Path=Name}"
           Background="LightYellow"
           VerticalAlignment="Top"/>
  </Grid>
</UserControl>

But are you sure you want to hide "Name" property from parents?

Anvaka
A: 

I've put the Name property just as sample. I'm trying to set Label Content in Window1.xaml.cs like:

View1.Name = "Casablanca";
Kamilos
A: 

Try the following binding, it should work:

<Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:View}}, Path=Name}" />

You should also define a xmlns:local="whatever_path_you_have" on the top of the file.

I also suggest renaming "Name" DP to something else to avoid name collusion.

Ugur Turan