Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:58:02+00:00 2026-05-29T09:58:02+00:00

Given the following XAML… <ComboBox x:Name=advisoriesComboBox DisplayMemberPath=Name ItemsSource={Binding Path=Advisories} SelectedItem={Binding Path=SelectedAdvisory} /> <Button Command={Binding

  • 0

Given the following XAML…

<ComboBox x:Name="advisoriesComboBox"
  DisplayMemberPath="Name" 
  ItemsSource="{Binding Path=Advisories}" 
  SelectedItem="{Binding Path=SelectedAdvisory}" />

<Button Command="{Binding Path=AddAdvisoryCommand}"
  CommandParameter="{Binding ElementName=advisoriesComboBox, Path=SelectedItem}"
  CommandTarget="{Binding ElementName=advisoriesComboBox}"
  Content="Add..." />

I am looking for a way to bind the ComboBox, Button and Command in such a way that when the value of the ComboBox changes, CanExecute is called on the Command. The net effect of what I want is to be able to enable and disable the Button based upon which item is selected in the list and I would prefer to do this using the ICommand interface.

I have done this in the past by using the “SelectedAdvisory” property on the VM and manually calling RaiseCanExecuteChanged on the command objects (I am using DelegateCommand instances from PRISM v4), but I feel sure that there is a better and cleaner way to do this using XAML only.

Thanks.

EDIT: Additionally, is there a simpler way to reference the ComboBox from the Button? I tried using RelativeSource PreviousData but could not get it to work, hence the usage x:Name.

Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:58:03+00:00Added an answer on May 29, 2026 at 9:58 am

    What you have setup now is what I would do. If the command can execute or not is a business rule, which means it should be handled from the ViewModel, not the View.

    The only difference is I make is to raise the CanExecuteChanged() in the PropertyChange event of your ViewModel, instead of the set method of SelectedAdvisory

    void MyViewModel()
    {
        this.PropertyChanged += MyViewModel_PropertyChanged;
    }
    
    void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch(e.PropertyName)
        {
            case "SelectedAdvisory":
                ((DelegateCommand)AddAdvisoryCommand).RaiseCanExecuteChanged();
                break;
        }
    }
    

    I prefer to keep logic out of my getters/setters if possible, and using the PropertyChanged event lets me see everything that happens when properties change in one location.

    Of course, if you’re using a RelayCommand instead of a DelegateCommand you don’t need to manually raise the CanExecuteChanged() when properties change since it does it automatically.

    You can also simplify your XAML a bit providing both share the same DataContext. The Button shouldn’t need to reference the ComboBox at all.

    <ComboBox
      DisplayMemberPath="Name" 
      ItemsSource="{Binding Path=Advisories}" 
      SelectedItem="{Binding Path=SelectedAdvisory}" />
    
    <Button Command="{Binding Path=AddAdvisoryCommand}"
      CommandParameter="{Binding SelectedAdvisory}"
      Content="Add..." />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following XAML fragment: <ComboBox ItemsSource={Binding ListOfItems} SelectedItem={Binding CurrentItem, Mode=TwoWay}/> It would seem
I have a ComboBox whose xaml is as follows <ComboBox Name=ComboBoxDiscussionType IsEnabled={Binding ElementName=ComboBoxDiscussionType, Path=Items.Count,
Given the following XAML: <TabControl DataContext={StaticResource mainVM} ItemsSource={Binding MovableVM.Docked, UpdateSourceTrigger=PropertyChanged}> <TabControl.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock
I have the following xaml: <ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <Button Content={Binding Name}></Button> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
Given the following bit of XAML <Border Name=Brder Visibility=Visible Width=10 Height=10 Background=Red></Border> <Button Content=Hide></Button>
given the following XAML code: <Canvas Name=MainView> <Canvas Name=TriangleElement Width=50 Height=50 Canvas.Left=110 Canvas.Top=100> <Canvas.RenderTransform>
Given the following XAML code that with a ListControl like behavior: <StackPanel> <ItemsControl Name=_listbox
Given the following canvas: <Canvas> <Canvas.LayoutTransform> <ScaleTransform ScaleX=1 ScaleY=1 CenterX=.5 CenterY=.5 /> </Canvas.LayoutTransform> <Button
Given the following XAML my goal is to keep the columns AAA, BBB, CCC
Given the following XAML markup, I would expect the text in the Hyperlink to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.