DataContext 不可访问,因此我猜绑定没有发生

发布于 2024-11-10 15:41:11 字数 3395 浏览 2 评论 0原文

   <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 技术交流群。

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

发布评论

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

评论(2

动次打次papapa 2024-11-17 15:41:11

GroupBoxHeader 是一个字符串(“Members”),因此 HeaderTemplate 中的 DataContext 也是一个字符串...并且 String 类型上没有 MemeberButtonSelected 属性,如错误消息中所述。您需要绑定到GroupBoxDataContext

...
<Grid HorizontalAlignment="Center" Width="320">
   <ToggleButton Name="LeftButton" Command="{Binding Path=DataContext.MemeberButtonSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}"/>
   <ToggleButton Name="RightButton" IsChecked="{Binding Path=DataContext.GroupSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}"/>
 </Grid>
...

The Header of your GroupBox is a string ("Members"), so the DataContext in the HeaderTemplate is also a string... and there is no MemeberButtonSelected property on type String, as mentioned in the error message. You need to bind to the DataContext of the GroupBox:

...
<Grid HorizontalAlignment="Center" Width="320">
   <ToggleButton Name="LeftButton" Command="{Binding Path=DataContext.MemeberButtonSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}"/>
   <ToggleButton Name="RightButton" IsChecked="{Binding Path=DataContext.GroupSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}"/>
 </Grid>
...
玻璃人 2024-11-17 15:41:11

您传递给它的 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.

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