2016-02-01 38 views
0

如何将“myCmdButton”按钮的命令绑定到ParameterVariableList,如“addBtn”do。 “addBtn”按预期工作。 我也想要其他按钮命令到我的ParameterVariableList类。如何将命令绑定到其“模板父项”

这里的XAML:

<DataTemplate DataType="{x:Type locale:ParameterVariableList}" > 
    <StackPanel Orientation="Vertical"> 

     <TextBlock Text="{Binding Description}"/> 
     <Button Content="add" Command="{Binding AddEntryCmd}" Name="addBtn" /> 

     <ListBox ItemsSource="{Binding ItemList, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" > 
      <ListBox.ItemTemplate> 
       <ItemContainerTemplate> 
        <StackPanel Orientation="Horizontal"> 

         <TextBlock Text="{Binding}" /> 

         <Button Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Command="{Binding RemoveEntryCmd, ???}" Name="myCmdButton" > 
          <Image Source="Resources/trash_16x16.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
         </Button> 

        </StackPanel> 
       </ItemContainerTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
</DataTemplate> 

我预计这个工作,但它并不:

Command="{Binding RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type locale:ParameterVariableList}}}" 

错误: System.Windows.Data错误:4:找不到源绑定引用'RelativeSource FindAncestor,AncestorType ='hmi.ParameterVariableList',AncestorLevel ='1''。 BindingExpression:路径= RemoveEntryCmd;的DataItem = NULL;目标元素是'Button'(Name ='');目标属性是 '命令'(类型 '的ICommand')

回答

0

这工作:

Command="{Binding DataContext.RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" 

几乎阿比纳夫的答案,但DataContext的失踪了。

0

这应该工作

Command="{Binding RemoveEntryCmd,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox}}} 
+0

编号System.Windows.Data错误:40:BindingExpression路径错误:在'object''''ListBox'(Name ='')'找不到'RemoveEntryCmd'属性。 BindingExpression:路径= RemoveEntryCmd; DataItem ='ListBox'(Name ='');目标元素是'Button'(Name ='');目标属性是'Command'(类型'ICommand')..试过AncestorType x:类型locale:ParamaterVariableList太..不工作.. – mxii

+0

对不起。先祖类型应该是DataTemplate使用的父类型控制。 例如,如果数据模板的父级是datagrid,则Datagrid应该是父级类型。 –

+0

该DataTemplate的父级是一个ListView,但ErrorMessage:System.Windows.Data错误:40:BindingExpression路径错误:'对象'''ListView'(Name ='listview_settings')'上找不到'RemoveEntryCmd'属性。 BindingExpression:路径= RemoveEntryCmd; DataItem ='ListView'(Name ='listview_settings');目标元素是'Button'(Name ='');目标属性是'命令'(类型'ICommand')..这对我来说甚至是不合逻辑的,导致它不是该ListView的属性? – mxii

相关问题