簡體   English   中英

如何調用數據模板中的按鈕?

[英]How can I invoke button in datatemplate?

我有工作按鈕的問題。 我使用 ObservableCollection 為顯示項目創建了模式,但我的按鈕不起作用。

在這里我顯示帶有 dosent 工作按鈕的項目

            <ItemsControl ItemsSource="{Binding Vehicles}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Task:VehcileTask/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

這是帶有 VehicleTask 的代碼顯示按鈕

            <Button Background="Red" Content="Delete"  Command="{Binding DeleteItem}">
            
        </Button>

這是調用的命令

        public ICommand DeleteItem { get; set; }

假設 DeleteItem 命令是主 ViewModel 的屬性,而不是單個數據項,那么您需要通過相關數據綁定來引用它。

Command="{Binding Path=DataContext.DeleteItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"

您還需要包含要刪除的項目作為 Button 的 CommandParameter

CommandParameter="{Binding}"

並在您的命令的相應方法中引用該參數值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM