0

Имеется ItemsControl, который выводит список radiobutton и textblock:

<ItemsControl x:Name="itemImages" Grid.Row="1">
    <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
              <WrapPanel/>
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
         <DataTemplate DataType="{x:Type local:ImageModel}">
              <StackPanel>
                   <RadioButton Style="{DynamicResource RadioButtonImageModel}" GroupName="1">
                   <Image Source="{Binding Path=Path}" Width="64"/>
                   </RadioButton>
                   <TextBlock HorizontalAlignment="Center" Text="{Binding Path=Name}"/>
              </StackPanel>
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Каким образом я могу отследить выбранный radiobutton в коде?

1 ответ 1

1

Если использование ItemsControl не принципиально, то просто используйте ListBox с таким же переопределением шаблона для данных. В RadioButton используйте привязку IsChecked к IsSelected ListBoxItem-a:

<ListBox x:Name="itemImages" Grid.Row="1" SelectedItem={Binding <!--здесь будет ваш выбранный объект -->}>
    <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
              <WrapPanel/>
         </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
         <DataTemplate DataType="{x:Type local:ImageModel}">
              <StackPanel>
                   <RadioButton Style="{DynamicResource RadioButtonImageModel}" GroupName="1" 
                            IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}">
                   <Image Source="{Binding Path=Path}" Width="64"/>
                   </RadioButton>
                   <TextBlock HorizontalAlignment="Center" Text="{Binding Path=Name}"/>
              </StackPanel>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.