簡體   English   中英

使用StaticResource中的ItemsSource將命令綁定到Viewmodel

[英]Binding Command to Viewmodel with ItemsSource from a StaticResource

我試圖將一個ItemsSource添加到MenuItem同時保持命令綁定到我的ViewModel(我的Window的DataContext)。 到目前為止,我還沒有想出辦法讓它發揮作用。 在添加ItemsSource之前,綁定很好。 我嘗試綁定的集合來自StaticResource 有人可以幫我解決這個問題嗎?

<MenuItem Command="{Binding OpenTeamPage}"
          DisplayMemberPath="Name"
          Header="Teams"
          ItemsSource="{Binding Teams,
                                Source={StaticResource Container}}" />

我試過使用它和它的變化沒有運氣:

Command="{Binding OpenTeamPage,
                  RelativeSource={RelativeSource AncestorType=Window},
                  Mode=Default}"

如果有人可以告訴我如何使用這個ItemsSource,同時仍然將我的命令綁定到我的ViewModel,我將非常感激。 我想我可以把Command放在我的Team模型中,但是如果可能的話我想避免使用它。

編輯:為了澄清我的問題,使用ItemsSource,ViewModel中的命令根本不會觸發。 沒有ItemsSource,該命令將觸發。 我希望能夠擁有ItemsSource並仍然能夠發出命令。

編輯:

public class GameContainer
{
    static GameContainer()
    {
        Teams = new ObservableCollection<Team>();
    }

    public static ObservableCollection<Team> Teams { get; set; } 
}

在App.xaml中:

<data:GameContainer x:Key="Container" />

程序啟動時將填充該集合。

一旦我完成這項工作,我的目標是將所選團隊傳遞給Viewmodel,希望通過CommandParameter,並顯示有關所選團隊的信息。

編輯:我在原帖中弄錯了。 來自Viewmodel的綁定集合也不起作用。

這是MenuItem的行為,具有Child MenuItem Item不會觸發Command ,它也不應該因為它沒有意義。 但是,如果您仍想在父項單擊上觸發命令,則有兩個選項

  1. 您可以在MenuItem上使用Interactivity Triggers來調用MouseDown事件上的命令

     <MenuItem DisplayMemberPath="Name" Header="Teams" ItemsSource="{Binding Teams, Source={StaticResource Container}}"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDown"> <cmd:EventToCommand Command="{Binding OpenTeamPage}" /> </i:EventTrigger> </i:Interaction.Triggers> </MenuItem> 
  2. 您可以為命令定義附加屬性並定義MenuItem MouseDown行為

      public static class MouseCommandBehavior { public static readonly DependencyProperty MouseDownCommandProperty = DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseCommandBehavior), new FrameworkPropertyMetadata(null, (obj, e) => OnMouseCommandChanged(obj, (ICommand)e.NewValue, false))); public static ICommand GetMouseDownCommand(DependencyObject d) { return (ICommand)d.GetValue(MouseDownCommandProperty); } public static void SetMouseDownCommand(DependencyObject d, ICommand value) { d.SetValue(MouseDownCommandProperty, value); } private static void OnMouseCommandChanged(DependencyObject d, ICommand command) { if (command == null) return; var element = (FrameworkElement)d; element.PreviewMouseDown += (obj, e) => command.Execute(null); } } } 

您可以在menuItem上設置此屬性值

<MenuItem local:MouseCommandBehavior.MouseDownCommand="{Binding OpenTeamPage}"
      DisplayMemberPath="Name"
      Header="Teams"
      ItemsSource="{Binding Teams,
      Source={StaticResource Container}}">

MenuItem不是葉節點,則不會執行其命令。 只有葉子的菜單項(沒有子項的項)正在執行命令。

這可能是由於慣例 - 當您單擊具有子項的項目時,您會立即顯示子項,否則從鼠標懸停到顯示的子項會有延遲。

雖然從(從UX的角度來看)對父母進行命令可能是一個壞主意,但它可能是:

<MenuItem DisplayMemberPath="Name"
          Header="{Binding OpenTeamPage}"
          ItemsSource="{Binding Teams, Source={StaticResource Container}}" >
    <MenuItem.HeaderTemplate>
        <DataTemplate>
            <!--Probably need to make this button transparent-->
            <Button Content="Teams"
                    Command="{Binding }"/>
        </DataTemplate>
    </MenuItem.HeaderTemplate>
    <!--This style is for the children to fire the same command as the parent-->
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command"
                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}, Path=Header}"/>
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

根據您的設計,您可能需要將按鈕設置為透明樣式。

暫無
暫無

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

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