简体   繁体   English

绑定到自定义XAML控件属性将不起作用

[英]Binding to a custom XAML control property won't work

I'm trying to create a Windows 10 UWP application with C# and XAML. 我正在尝试使用C#和XAML创建Windows 10 UWP应用程序。 At this point I'm quite stuck with data binding in item templates. 在这一点上,我对项目模板中的数据绑定非常感兴趣。

So basically, I have a ViewModel, that is bound to the XAML controls. 所以基本上,我有一个ViewModel,它绑定到XAML控件。

I want to bind the data model to a property of a custom control. 我想将数据模型绑定到自定义控件的属性。 The thing is that some things work, some don't and I can't really understand why. 问题是有些事情行之有效,有些却行不通,我真的不明白为什么。

So this binding works: 因此,此绑定有效:

  <GridView   ItemsSource="{Binding MediaElementId}" >
       <GridView.ItemTemplate>
           <DataTemplate>
               <TextBlock Text="{Binding}" />
           </DataTemplate>
        </GridView.ItemTemplate>
   </GridView>

But when i put other things insted of the TextBlock, it won't work. 但是,当我将其他东西插入TextBlock时,它将无法工作。

For example these bindings won't work : 例如,这些绑定将不起作用

<TextBlock  Text="{Binding  Path=.}"/>
 <controls:CustomControl Test="{Binding Path=., UpdateSourceTrigger=PropertyChanged}"  />
 <controls:CustomControl Test="{Binding Path=., RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/>
 <controls:CustomControl Test="{Binding}"/>

So, I basically found documentation, that {Binding} is equivalent with {Binding Path=.}, but in this example it doesn't really seem that way. 因此,我基本上找到了文档,即{Binding}与{Binding Path =。}等效,但是在此示例中,它似乎并非如此。

You can try it yourself in a project, where I isolated the issue: https://bitbucket.org/MrGreeny/pivottestapp/src/4edee74acfac6dbb4f9f042acaf389cc6fd90a31?at=master 您可以在一个我发现问题的项目中自己尝试: https : //bitbucket.org/MrGreeny/pivottestapp/src/4edee74acfac6dbb4f9f042acaf389cc6fd90a31?at=master

Edit: The property is a pretty simple one: 编辑:该属性是一个非常简单的属性:

    private string test;

    public string Test
    {
        get { return null; }
        set
        {
            Debug.WriteLine(value);
           //What i think should be here is for example textblock.Text = value
        }
    }

So, basically all I had to do was use DependencyProperty instead of a normal property. 因此,基本上我要做的就是使用DependencyProperty而不是常规属性。 Here is the working solution: 这是有效的解决方案:

First the control inside the DataTemplate : 首先是DataTemplate内部的控件:

<GridView ItemsSource="{Binding MediaElementId}" >
   <GridView.ItemTemplate>
      <DataTemplate>
         <local:CustomControl TextBlockContent="{Binding}" />
      </DataTemplate>
   </GridView.ItemTemplate>
 </GridView>

Then in the custom control xaml.cs file: 然后在自定义控件xaml.cs文件中:

First is the wrapper exposing the DependencyProperty : 首先是包装程序公开DependencyProperty

public string TextBlockContentProperty
{
    get { return (string)GetValue(TextBlockContentPropertyProperty); }
    set { SetValue(TextBlockContentPropertyProperty, value); }
}

Then the DependencyProperty itself. 然后是DependencyProperty本身。

     public static readonly DependencyProperty TextBlockContentPropertyProperty =
        DependencyProperty.Register(
           "TextBlockContentProperty", 
           typeof(string), 
           typeof(CustomControl), 
           new PropertyMetadata(0, new PropertyChangedCallback(OnTextChanged)) );

Note, that the last argument contains the connection to the OnTextChanged function, wherewe have access to the object instance. 请注意,最后一个参数包含与OnTextChanged函数的连接,在这里我们可以访问对象实例。 All of the above are static , and we can't use them to change the instance of the object. 以上所有都是static ,我们不能使用它们来更改对象的实例。

Here is how I implemented the instance specific logic. 这是我实现实例特定逻辑的方式。 TextBlockContent is the normal property, that exposes the Text property of a TextBlock , that is the content of the custom control. TextBlockContent是常规属性,它公开TextBlockText属性,即自定义控件的内容。

    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CustomControl cc = d as CustomControl;
        string content = (string)e.NewValue;
        cc.TextBlockContent = content;
    }

Thanks to everyone, who answered my original question. 感谢所有回答我最初问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM