如何将 属性 个动态创建的用户控件组件绑定到另一个组件?

How to bind property of dynamically created components of a user control to another component?

我有一个用户控件,它基于列表创建一组单选按钮。单选按钮是使用数据模板创建的。

<UserControl.Resources>
        <SelectableItem:SelectableItem x:Key="vm"></SelectableItem:SelectableItem>
        <src:RadioButtonCheckedConverter x:Key="RadioButtonCheckedConverter" />
        <CollectionViewSource
                Source="{Binding Source={x:Static Application.Current}, Path=ItemDescription}"
                x:Key="ListingDataView" />
        <DataTemplate x:Key="GroupingHeaderTemplate">
            <TextBlock Text="{Binding Path=Name}" Style="{StaticResource GroupHeaderStyle}"/>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ItemsControl Name="RadioGroup" AutomationProperties.Name="RadioGroup" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton
                                 GroupName="{Binding Path=ItemType}"
                                 Content="{Binding Path=ItemDescription}"
                                 FlowDirection="RightToLeft"
                                 IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Style="{DynamicResource CustomRadioButton}" Margin="20,0" Checked="RadioButton_Checked" Tag="{Binding Path=ItemDescription, Mode=TwoWay}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
    </Grid>
</UserControl>

我在 window 中使用它,我需要根据选定的单选按钮更改组件(堆栈面板)的可见性。

<uc:CommonRadioButtonGroup x:Name="SelectionButtonsGroup" ></uc:CommonRadioButtonGroup>

我正在尝试使用样式触发器更改可见性。

<Style x:Key="spStyle" TargetType="StackPanel" >
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static Local:EngineModes.PresentMode}}" Value="Stop">
                    <Setter Property="StackPanel.Visibility" Value="Hidden" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Source={x:Static Local:EngineModes.PresentMode}}" Value="Wait">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Source={x:Static Local:EngineModes.PresentMode}}" Value="Go">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>                
            </Style.Triggers>
        </Style>

我想不出一种方法来实现这个视图模型。我试过这个:

public class EngineModes : INotifyPropertyChanged
    {
        public static List<SelectableItem> Modes { get; set; } = new List<SelectableItem>();
                public static string PresentMode { get; set; }

  
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyInfo)
        {
            App.Current.Dispatcher.BeginInvoke((Action)(() =>
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyInfo));
            }
            ));
        }
    }

其中“模式”是单选按钮的选项。但这根本行不通。

最终,在使用单选按钮选择模式时,必须修改堆栈面板的可见性。 请评论代码的正确性。

编辑: 这是在代码隐藏中添加的用户控件的 ItemSource:

 SelectionButtonsGroup.RadioGroup.ItemsSource = EngineModes.Modes;

        this.DataContext = EngineModes.PresentMode;
        

我将样式更新为

<Style x:Key="sprecStyle" TargetType="StackPanel">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={StaticResource engineModes}, Path=PresentMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="Stop">
                    <Setter Property="StackPanel.Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Source={StaticResource engineModes}, Path=PresentMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="Wait">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Source={StaticResource engineModes}, Path=PresentMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="Go">
                    <Setter Property="StackPanel.Visibility" Value="Collapsed" />
                </DataTrigger>  
            </Style.Triggers>
        </Style>

并为静态 属性 更改添加了单独的通知事件:

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
     = delegate { };
    public static void NotifyStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
    }

并在更新引擎模式的地方调用它。

EngineModes.NotifyStaticPropertyChanged("PresentMode");

瞧!成功了。