DataContext 不可访问,因此我猜绑定没有发生
<Grid>
<CheckBox Content="Select All" IsChecked="{Binding Path=SelectAll}"/>
<TextBlock Grid.Column="1" Text="Filter by:" />
<RadioButton IsChecked="{Binding Path=All}" GroupName="filterGroup" Content="All" />
<RadioButton IsChecked="{Binding Path=NShared}" GroupName="filterGroup" Content="Not Shared" />
</Grid>
<GroupBox Header="Members" Style="{StaticResource CenteredHeaderGroupBoxStyle}" Width="330">
<GroupBox.HeaderTemplate>
<DataTemplate>
<Border Width="320">
<Grid HorizontalAlignment="Center" Width="320">
<ToggleButton Name="LeftButton" Command="{Binding Path=MemeberButtonSelected}"/>
<ToggleButton Name="RightButton" IsChecked="{Binding Path=GroupSelected}"/>
</Grid>
</Border>
</DataTemplate>
</GroupBox.HeaderTemplate>
<GroupItem>
<Border>
<ListBox Name="GroupMemberList">
<ListBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=GroupSelected}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Path=GroupsoftheCase}"/>
<Setter Property="ListBox.ItemTemplate" Value="{StaticResource GroupListTemplate}"/>
<Setter Property="ListBox.SelectedValue" Value="{Binding Path=SelectedGroups}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=MemberSelected}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Path=MembersoftheCase}"/>
<Setter Property="ListBox.ItemTemplate" Value="{StaticResource MembersListTemplate}"/>
<Setter Property="ListBox.SelectedValue" Value="{Binding Path=SelectedMembers}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
</Border>
</GroupItem>
</GroupBox>
ToggleButton 绑定不起作用,属性/命令存在于视图的 DataContext 中,
但输出显示
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1399923548) 上找不到“MemeberButtonSelected”属性。 BindingExpression:Path=MemeberButtonSelected; DataItem='字符串'(哈希码=-1399923548);目标元素是“ToggleButton”(名称=“”);目标属性是“Command”(类型“ICommand”)
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1399923548) 上找不到“GroupSelected”属性。 BindingExpression:Path=GroupSelected; DataItem='字符串'(哈希码=-1399923548);目标元素是“ToggleButton”(名称=“”);目标属性是“IsChecked”(类型“Nullable`1”)
相对源
我还尝试了切换按钮之一的
IsChecked="{Binding Path=MemberSelected, RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}}"
,其不使用仅输出中的字符串将更改为对象仅此而已
<Grid>
<CheckBox Content="Select All" IsChecked="{Binding Path=SelectAll}"/>
<TextBlock Grid.Column="1" Text="Filter by:" />
<RadioButton IsChecked="{Binding Path=All}" GroupName="filterGroup" Content="All" />
<RadioButton IsChecked="{Binding Path=NShared}" GroupName="filterGroup" Content="Not Shared" />
</Grid>
<GroupBox Header="Members" Style="{StaticResource CenteredHeaderGroupBoxStyle}" Width="330">
<GroupBox.HeaderTemplate>
<DataTemplate>
<Border Width="320">
<Grid HorizontalAlignment="Center" Width="320">
<ToggleButton Name="LeftButton" Command="{Binding Path=MemeberButtonSelected}"/>
<ToggleButton Name="RightButton" IsChecked="{Binding Path=GroupSelected}"/>
</Grid>
</Border>
</DataTemplate>
</GroupBox.HeaderTemplate>
<GroupItem>
<Border>
<ListBox Name="GroupMemberList">
<ListBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=GroupSelected}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Path=GroupsoftheCase}"/>
<Setter Property="ListBox.ItemTemplate" Value="{StaticResource GroupListTemplate}"/>
<Setter Property="ListBox.SelectedValue" Value="{Binding Path=SelectedGroups}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=MemberSelected}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Path=MembersoftheCase}"/>
<Setter Property="ListBox.ItemTemplate" Value="{StaticResource MembersListTemplate}"/>
<Setter Property="ListBox.SelectedValue" Value="{Binding Path=SelectedMembers}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
</Border>
</GroupItem>
</GroupBox>
The ToggleButton Binding is Not working the Properties/Commands are existing in the DataContext of the View
but the output says
System.Windows.Data Error: 40 : BindingExpression path error: 'MemeberButtonSelected' property not found on 'object' ''String' (HashCode=-1399923548)'. BindingExpression:Path=MemeberButtonSelected; DataItem='String' (HashCode=-1399923548); target element is 'ToggleButton' (Name=''); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 40 : BindingExpression path error: 'GroupSelected' property not found on 'object' ''String' (HashCode=-1399923548)'. BindingExpression:Path=GroupSelected; DataItem='String' (HashCode=-1399923548); target element is 'ToggleButton' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
i have also tried the Relative Source
as
IsChecked="{Binding Path=MemberSelected, RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}}"
for one of the Toggle buttons and its of no Use only the String in the output will change to object thats all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GroupBox
的Header
是一个字符串(“Members”),因此HeaderTemplate
中的DataContext
也是一个字符串...并且String
类型上没有MemeberButtonSelected
属性,如错误消息中所述。您需要绑定到GroupBox
的DataContext
:The
Header
of yourGroupBox
is a string ("Members"), so theDataContext
in theHeaderTemplate
is also a string... and there is noMemeberButtonSelected
property on typeString
, as mentioned in the error message. You need to bind to theDataContext
of theGroupBox
:您传递给它的 DataContext 对象类型是什么?您的输出显示字符串,因此您在某个地方得到了错误的数据。尝试在每次更新您绑定的数据的实例后设置一个断点。或者,如果您有 Mole,您可以使用它直接查看项目数据上下文。
What is the DataContext object type you are passing into that? Your output says string, so somewhere you are getting the wrong data. Try setting a breakpoint after every instance where whatever data you are binding to is updated. Alternatively, if you have Mole you can use that to look at items datacontext directly.