Frage

Hello I am a new WPF/MVVM programmer and having trouble with a MenuItem.

I have a menu who's ItemsSourced are binded to an object I created;

        <Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
        <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="IsCheckable" Value="True"/>
                    <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </Menu>

When I run this I can see all my items in the menu but my property IsSelected isn't getting updated.

    public bool IsSelected
    {
        get { return _isSelected; }
        set 
        { 
            _isSelected = value;
            GetProfileConfigInfo();        
        }
    }

If I take the style container out and put the IsCheckable and IsChecked values in line with the MenuItem....

<MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" IsCheckable="True" IsChecked="{Binding IsSelected}" />

my property IsSelected gets updated but I can't see any of the items in my menu just the header Profiles.

And idea on what I'm doing wrong????

War es hilfreich?

Lösung

In your setter for IsSelected, you need to call OnPropertyChanged("IsSelected").

I think this could fix your problem.

I copied your exact code (below) into Blend, and created a sample data source with your exact property names, and the checkboxes in the menu worked properly.

<Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
    <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
        <MenuItem.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="IsCheckable" Value="True"/>
                <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
</Menu>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top