Wpf datatemplate不绑定属性值 [英] Wpf datatemplate does not binding property value

查看:149
本文介绍了Wpf datatemplate不绑定属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带标题的组框应该在xaml中显示带按钮的文本。使用我的viewmodel中定义的IsbtnVisibility属性设置Header按钮的可见性。

I have one group box with header should display a text with button in xaml. setting visibility of Header button using IsbtnVisibility property defined in my viewmodel.

IsbtnVisibility 

是bool属性。如果它是假的,按钮应该是collpase但它没有按预期工作。有没有办法解决它。



我尝试过的事情:



is bool property. when it is false the button should collpase but it is not working as expected. Is there any way to solve it.

What I have tried:

<GroupBox Grid.Row="0" Padding="3,5,3,3">

                                              <GroupBox.HeaderTemplate>
                                                  <DataTemplate>
                                                      <StackPanel Orientation="Horizontal" >
                                                          <TextBlock Text="Sample Header " FontWeight="Bold"/>
                                                          <UserCntrl:RoundedButton x:Name="btnRoundedButton" Visibility="{Binding Path=IsbtnVisibility,Converter={StaticResource FalseToCollapsedConverter}}"/>
                                                      </StackPanel>
                                                  </DataTemplate>
                                              </GroupBox.HeaderTemplate>
                                              </GroupBox>

推荐答案

HeaderTemplate 有自己的 DataContext ,因此您无法直接绑定到父 DataContext 上的属性。如果检查输出窗口,您将看到错误,告诉您找不到该属性。



您需要使用 RelativeSource 绑定以访问父 DataContext

The HeaderTemplate has its own DataContext, so you can't bind directly to properties on the parent DataContext. If you examine the output window, you will see errors telling you that the property was not found.

You need to use a RelativeSource binding to access the parent DataContext:
<UserCntrl:RoundedButton x:Name="btnRoundedButton" 
    Visibility="{Binding Path=DataContext.IsbtnVisibility,
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Converter={StaticResource FalseToCollapsedConverter}}"
/>



WPF GroupBox HeaderTemplate和DataBinding - 堆栈溢出 [ ^ ]


在Visual Studio中执行项目时,您是否在输出窗口中看到BindingExpression错误?



c# - WPF Error 40 BindingExpression路径错误:'object'上找不到属性 - Stack Overflow [ ^ ]



如果是,那可能是你的意思我想看看修复。



如果不是,你可以在你遇到问题的绑定中使用布尔到布尔转换器。只需在转换器中设置一个断点,即可查看在视图和数据源之间传递的实际值...



WPF教程|调试数据绑定问题 [ ^ ]
Do you see a BindingExpression error being displayed in the Output Window when you execute your project in Visual Studio?

c# - WPF Error 40 BindingExpression path error: property not found on 'object' - Stack Overflow[^]

If Yes, that is probably something you want to look into fixing.

If No, You can use a Boolean to Boolean converter in the binding that you are having trouble with. Just set a breakpoint in the converter to see the actual values being passed forth and back between view and data source...

WPF Tutorial | Debug DataBinding Issues[^]


这篇关于Wpf datatemplate不绑定属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆