Link to home
Start Free TrialLog in
Avatar of Matt Cartlidge
Matt CartlidgeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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.

User generated image
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

SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Matt Cartlidge

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great! Thank you!

What is the purpose of => ?

I haven't seen that used before, but I am relatively new to C#
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All sorted, thanks!