bakala12
bakala12

Reputation: 378

Menu item with exactly one checked value

I want to have in my application a context menu "DefaultPrinter" which has some binded childs (another MenuItems) representing printer's names. Each child has a property IsCheckable set to true.

<MenuItem Header="DefaultPrinter" ItemsSource="{Binding AllPrinters}">
     <MenuItem.ItemContainerStyle>
          <Style TargetType="{x:Type MenuItem}">
              <Setter Property="IsCheckable" Value="True"/>
          </Style>
     </MenuItem.ItemContainerStyle>
</MenuItem>

However, of course I want allow only one printer to be checked (this code doesn't suport that). I looked for solution in the Internet, but didn't find anything helpful. The problem is that MenuItem derieves from ItemsControl and since that it has no concept of items selection (like Selector). I can try to write my own selection, but first, I would like to know is there better and simplier solution for achieving that goal.

Any help will be appreciated.

Upvotes: 0

Views: 1064

Answers (1)

brunnerh
brunnerh

Reputation: 184516

Could abuse a hidden RadioButton:

<MenuItem.ItemTemplate>
    <DataTemplate>
        <Grid>
            <RadioButton GroupName="Printers"
                    IsChecked="{Binding IsChecked, RelativeSource={RelativeSource AncestorType=MenuItem}}"
                    Visibility="Collapsed"/>
            <TextBlock Text="{Binding FullName}"/>
        </Grid>
    </DataTemplate>
</MenuItem.ItemTemplate>

Upvotes: 1

Related Questions