【问题标题】:DataContext is Not Accesible so the Binding is not happening i guessDataContext 不可访问,所以我猜绑定没有发生
【发布时间】:2011-05-31 12:29:23
【问题描述】:
   <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='String' (HashCode=-1399923548);目标元素是 'ToggleButton' (Name='');目标属性是“命令”(输入“ICommand”)


System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1399923548)上找不到“GroupSelected”属性。 BindingExpression:Path=GroupSelected; DataItem='String' (HashCode=-1399923548);目标元素是 'ToggleButton' (Name='');目标属性为“IsChecked”(类型为“Nullable`1”)

我也尝试过相对来源

作为

 IsChecked="{Binding Path=MemberSelected, RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}}"

对于其中一个切换按钮及其不使用仅输出中的字符串将更改为全部对象

【问题讨论】:

  • 你确定没有错别字吗? MemeberButtonSelected 看起来像是一个错字。此外,在相对路径示例中,您将其命名为 MemberSelected

标签: .net wpf data-binding binding datatemplate


【解决方案1】:

您的GroupBoxHeader 是一个字符串(“成员”),因此HeaderTemplate 中的DataContext 也是一个字符串...并且@987654326 类型上没有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>
...

【讨论】:

    【解决方案2】:

    您传递给它的 DataContext 对象类型是什么?您的输出显示字符串,因此您在某处得到了错误的数据。尝试在您绑定到的任何数据被更新的每个实例之后设置一个断点。或者,如果您有 Mole,您可以使用它直接查看项目数据上下文。

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 2020-04-04
      • 2016-03-27
      • 2020-07-12
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2012-02-08
      相关资源
      最近更新 更多