WPF binding user control with data in C# code

前提是你 提交于 2019-12-24 06:39:03

问题


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?


回答1:


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?




回答2:


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

DataContext="{Binding RelativeSource={RelativeSource Self}}"



回答3:


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

View1.Name = "Casablanca";



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1477347/wpf-binding-user-control-with-data-in-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!