Wpf datatemplate不绑定属性值

编程入门 行业动态 更新时间:2024-10-21 09:10:15
本文介绍了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不绑定属性值

本文发布于:2023-11-11 23:27:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   Wpf   datatemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!