このままだと冗長なので、隣接する要素に対してhiddenその他はvisibleみたいな処理に変更したいです。
チェックでHidden
なのですか!?
アンチェックでCollapsed
なら既存のコンバータを使えるんですが...
BooleanToVisibilityConverter Class (System.Windows.Controls) | Microsoft Learn
bool
を受け取ってVisibility
を返すIValueConverter
を実装してください。
同じようなことを繰り返し書くのがイヤな場合は、ListBox
等を使えば(繰り返し部分は)スッキリ書けます(が、余計長くなるでしょうw
xml
1<Window
2 x:Class="Q59713.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:local="clr-namespace:Q59713"
6 Width="800"
7 Height="450"
8 ThemeMode="System">
9 <Window.Resources>
10 <local:MyConverter x:Key="MyConverter" />
11 <Style TargetType="Border">
12 <Setter Property="MinHeight" Value="16" />
13 </Style>
14 </Window.Resources>
15 <StackPanel Margin="4">
16
17 <RadioButton x:Name="red" Content="Red" />
18 <Border Background="Red" Visibility="{Binding IsChecked, ElementName=red, Converter={StaticResource MyConverter}}" />
19
20 <RadioButton x:Name="green" Content="Green" />
21 <Border Background="Green" Visibility="{Binding IsChecked, ElementName=green, Converter={StaticResource MyConverter}}" />
22
23 <RadioButton x:Name="blue" Content="Blue" />
24 <Border Background="Blue" Visibility="{Binding IsChecked, ElementName=blue, Converter={StaticResource MyConverter}}" />
25
26 <RadioButton x:Name="pink" Content="Pink" />
27 <Border Background="Pink" Visibility="{Binding IsChecked, ElementName=pink, Converter={StaticResource MyConverter}}" />
28
29
30 <Separator Margin="4" />
31
32
33 <ListBox HorizontalContentAlignment="Stretch">
34 <ListBox.ItemContainerStyle>
35 <Style TargetType="ListBoxItem">
36 <Setter Property="Template">
37 <Setter.Value>
38 <ControlTemplate TargetType="ListBoxItem">
39 <StackPanel>
40 <RadioButton
41 x:Name="radioButton"
42 Content="{Binding Tag}"
43 IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}" />
44 <ContentPresenter x:Name="contentPresenter" />
45
46 <!--<ContentPresenter Visibility="{Binding IsChecked, ElementName=radioButton, Converter={StaticResource MyConverter}}" />-->
47 <!-- Converter使うなら↓のTriggerはいらないが -->
48 </StackPanel>
49 <ControlTemplate.Triggers>
50 <DataTrigger Binding="{Binding IsChecked, ElementName=radioButton}" Value="True">
51 <Setter TargetName="contentPresenter" Property="Visibility" Value="Hidden" />
52 </DataTrigger>
53 </ControlTemplate.Triggers>
54 </ControlTemplate>
55 </Setter.Value>
56 </Setter>
57 </Style>
58 </ListBox.ItemContainerStyle>
59 <Border Background="Red" Tag="Red" />
60 <Border Background="Green" Tag="Green" />
61 <Border Background="Blue" Tag="Blue" />
62 <Border Background="Pink" Tag="Pink" />
63 </ListBox>
64
65 </StackPanel>
66</Window>
cs
1using System.Globalization;
2using System.Windows;
3using System.Windows.Data;
4
5namespace Q59713;
6
7public partial class MainWindow : Window
8{
9 public MainWindow() => InitializeComponent();
10}
11
12public sealed class MyConverter : IValueConverter
13{
14 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
15 => (bool)value ? Visibility.Hidden : Visibility.Visible;
16
17 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
18}

質問者は全体的にXY問題が多く、適切な回答が付かないですね...
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。