WPF binding user control with data in C# code

2019-08-15 20:48发布

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?

5条回答
Bombasti
2楼-- · 2019-08-15 21:00

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-15 21:01

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?

查看更多
Luminary・发光体
4楼-- · 2019-08-15 21:04

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

View1.Name = "Casablanca";
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-08-15 21:16

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.

查看更多
不美不萌又怎样
6楼-- · 2019-08-15 21:18

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

DataContext="{Binding RelativeSource={RelativeSource Self}}"
查看更多
登录 后发表回答