2016-02-01 37 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

相關問題