DataTrigger,通过 TemplatedParent 绑定到嵌套属性

发布于 2024-10-31 12:02:11 字数 2329 浏览 0 评论 0原文

根据msdn,它应该是将某些内容绑定到嵌套属性是完全合法且可能的:

<Binding Path="propertyName.propertyName2" .../>
<Binding Path="propertyName.propertyName2.propertyName3" .../>

但就我而言,情况并非如此...

我有一个自定义控件 MyControl,带有依赖属性 ViewModel< /code>:

    public static DependencyProperty ViewModelProperty = DependencyProperty.Register(
        "ViewModel", typeof(IViewModel), typeof(MyControl));

    public IViewModel ViewModel
    {
        get { return (IViewModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

在控件模板中,我尝试绑定到该视图模型中的属性:

 <Style TargetType="{x:Type my:MyControl}">
   <Setter Property="Template">
     <Setter.Value>
       <ControlTemplate TargetType="{x:Type my:MyControl}">
         <Grid>
           <TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>
           <Button x:Name="MyButton" Content="Visible by trigger" Visibility="Collapsed" />
         </Grid>
       <ControlTemplate.Triggers>
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.ButtonVisible}" Value="True">
           <Setter TargetName="MyButton" Property="Visibility" Value="Visible" />
         </DataTrigger>            
      .../>

在视图模型本身中,我有一个 preoperty 文本如下:

    public string Text
    {
        get { return m_text; }
        set
        {
            m_text = value;
            OnPropertyChanged("Text");
        }
    }

    public bool ButtonVisible
    {
      get { return m_buttonVisible; }
      set 
     { 
       m_buttonVisible = value; 
       OnPropertyChanged("ButtonVisible"); }
     }

我没有收到绑定错误,但事情没有发生...

任何线索?

编辑 看起来绑定工作了一半。当编辑框中的文本更改时,我的 Text 属性被设置,但如果在代码中设置 Text-property,用户界面将不会更新。

编辑2
看起来我在发布之前简化案例的第一次尝试有点成功......正如@Erno 指出的那样,我发布的代码似乎工作正常。

我又查看了原始代码,并向场景添加了触发器。原始代码使用触发器在给定条件下显示用户界面的部分内容。这些也绑定到嵌套属性。我现在认为这些触发器未能触发。我已经更新了代码。如果它仍然没有显示出什么问题,我可以在某个地方发布一个示例应用程序。

According to msdn, it should be perfectly legal, and possible, to bind something to a nested property:

<Binding Path="propertyName.propertyName2" .../>
<Binding Path="propertyName.propertyName2.propertyName3" .../>

In my case, it's not so, though...

I have a custom control, MyControl, with a dependency property ViewModel:

    public static DependencyProperty ViewModelProperty = DependencyProperty.Register(
        "ViewModel", typeof(IViewModel), typeof(MyControl));

    public IViewModel ViewModel
    {
        get { return (IViewModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

and in the control template, I try to bind to properties in that viewmodel:

 <Style TargetType="{x:Type my:MyControl}">
   <Setter Property="Template">
     <Setter.Value>
       <ControlTemplate TargetType="{x:Type my:MyControl}">
         <Grid>
           <TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>
           <Button x:Name="MyButton" Content="Visible by trigger" Visibility="Collapsed" />
         </Grid>
       <ControlTemplate.Triggers>
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.ButtonVisible}" Value="True">
           <Setter TargetName="MyButton" Property="Visibility" Value="Visible" />
         </DataTrigger>            
      .../>

In the viewmodel itself, I have a preoperty Text as follow:

    public string Text
    {
        get { return m_text; }
        set
        {
            m_text = value;
            OnPropertyChanged("Text");
        }
    }

    public bool ButtonVisible
    {
      get { return m_buttonVisible; }
      set 
     { 
       m_buttonVisible = value; 
       OnPropertyChanged("ButtonVisible"); }
     }

I get no bind errors, but things doesn't happend...

Any clues?

Edit
It looks like the bindings work half way. When the text is changed in the editbox, my Text property is set, but if the Text-property is set in code, the ui won't update.

Edit 2
Looks like my first attempt at simplifying the case before posting was a little to successful... As @Erno points out, the code that I posted seems to work OK.

I have looked at the original code some more, and added a trigger to the scenario. The original code uses triggers to show parts of the ui at given conditions. These are also binded to nested properties. I now think that these triggers fail to trigger. I have updated the code. If it still doesn't show whats wrong, I can post a sample application some where.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-11-07 12:02:11

缺少逗号:

<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>

EDIT

将 Mode=TwoWay 添加到绑定:

<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text, Mode=TwoWay}"/>

EDIT2

明白了!我可以重现并修复它。

在绑定中将 TemplatedParent 替换为 Self。
阅读此说明

There is a comma missing:

<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>

EDIT

Add Mode=TwoWay to the binding:

<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text, Mode=TwoWay}"/>

EDIT2

Got it! I could reproduce and fix it.

Replace the TemplatedParent with Self in the binding.
Read this explanation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文