We help IT Professionals succeed at work.

XAML/WPF: How to get XAML to re-evaluate when using a ComboBox  in a ListView

Matt Cartlidge
on
418 Views
Last Modified: 2018-03-19
Hi,

I'm finding it tricky to find an answer for this as maybe I'm not wording my searches correctly.

I've got some fairly simple XAML whichs presents ListView items based on a class which uses the usual ObservableCollection and INotifyChanged.

ListView Combo drop down
Above you see one row in the Grid and the "Channel" combo is open with the options. You can't see it clearly (this is another problem I've got) but the last 8 items are actually disabled - only the first 4 items are enabled. Also the row itself is difficult to see - too bright - which is ANOTHER problem I have (asked separately but no answers as yet).

The XAML takes care of the enable/disable status depending on if the "Mode" combo is "Stereo" or "Mono". If "Stereo" is selected then the first 4 items are enabled and the rest are disabled.

I want the selection of "Mono" to do the opposite - disable the first 4 items and enable the rest.

My question is, how do I get the XAML to re-evaluate the "Channel" ComboBox? Or do I need to manage this in the code somehow?

Here's the ListView code.

                                    <ListView x:Name="ListViewOutputs" Style="{DynamicResource StyleListView}">
                                        <ListView.View>
                                            <GridView>
                                                <GridViewColumn Header="Enabled">
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <CheckBox IsChecked="{Binding Enabled, Mode=TwoWay}" Style="{DynamicResource StyleCheckBox}"/>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                                </GridViewColumn>
                                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                                                <GridViewColumn Header="Mode">
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <ComboBox x:Name="ComboBoxOutputMode" SelectionChanged="ComboBoxOutputMode_SelectionChanged" SelectedValuePath="Content" SelectedValue="{Binding Mode, Mode=TwoWay}" Style="{DynamicResource ComboBoxStyle}">
                                                                <ComboBoxItem Content="Stereo"/>
                                                                <ComboBoxItem Content="Mono"/>
                                                            </ComboBox>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                                </GridViewColumn>
                                                <GridViewColumn Header="Monitor">
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <CheckBox IsChecked="{Binding Monitor, Mode=TwoWay}" Style="{DynamicResource StyleCheckBox}"/>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                                </GridViewColumn>
                                                <GridViewColumn Header="Channel">
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <ComboBox x:Name="ComboBoxOutputChannel" DropDownOpened="ComboBoxOutputChannel_DropDownOpened"  SelectedValuePath="Content" SelectedValue="{Binding ChannelString, Mode=TwoWay}" Style="{DynamicResource ComboBoxStyle}">
                                                                <ComboBoxItem IsEnabled="{Binding IsModeStereo}" Content="Front"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeStereo}" Content="Rear"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeStereo}" Content="Center/Subwoofer"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeStereo}" Content="Side"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Front Left"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Front Right"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Rear Left"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Rear Right"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Center"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Subwoofer"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Side Left"/>
                                                                <ComboBoxItem IsEnabled="{Binding IsModeMono}" Content="Side Right"/>
                                                            </ComboBox>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                                </GridViewColumn>
                                                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
                                            </GridView>
                                        </ListView.View>
                                    </ListView>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Matt CartlidgeSenior Infrastructure Engineer

Author

Commented:
Thanks for your response.

It is becoming clearer now why this isn't working as I'd expect.

My code for the IsModeStereo method :-

        public bool IsModeStereo
        {
            get
            {
                if (Mode == MCOutputMode.Stereo) { return true; }
                return false;
            }
        }

Open in new window


You can probably guess what the IsModeMono method looks like so I won't post that.

This is the MCOutputMode which determines the result of IsModeMono and IsModeStereo.

        private MCOutputMode _Mode;
        public MCOutputMode Mode
        {
            get { return _Mode; }
            set
            {
                _Mode = value;
                OnPropertyChanged();
            }
        }

Open in new window


I've added the OnPropertyChanged() call for both IsModeMono and IsModeStereo. However, this causes an overflow, assuming because  they are stuck in a loop calling eachother.

Can you see what I'm doing wrong here? Happy to share more code if you think it's relevant.
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Matt CartlidgeSenior Infrastructure Engineer

Author

Commented:
Works great! Thank you!

What is the purpose of => ?

I haven't seen that used before, but I am relatively new to C#
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Matt CartlidgeSenior Infrastructure Engineer

Author

Commented:
All sorted, thanks!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.