bugfix> c# > 投稿

Prismの InteractionRequestTrigger を使用していますいくつかのUserControlsで、独自のウィンドウコンテンツを使用して通知を表示します。 TestControl 3つの RadioButtons を含む 。 TestControl のVM作成時最初のラジオボタンで選択を設定します。

問題は、表示するときに最終 ウィンドウ、ラジオボタンは あるべきように選択。ただし、前に作成された他のすべてのウィンドウには選択範囲がありません。私は私の EnumToBooleanConverterConvertBack 関数が呼び出され、次のユーザーコントロールの最初のラジオボタンが選択されたときに再び選択を解除します。など...理由を知っていますか?これを解決するには?

この問題を再現する簡単なコードを次に示します。

メインウィンドウ:

 <Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:prism="http://www.codeplex.com/prism"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:vm="clr-namespace:WpfApp1.ViewModels" Title="MainWindow">
<StackPanel>
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding StretchModeRequest1}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <local:TestControl/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
        <prism:InteractionRequestTrigger SourceObject="{Binding StretchModeRequest2}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <local:TestControl/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
        <prism:InteractionRequestTrigger SourceObject="{Binding StretchModeRequest3}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <local:TestControl/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <Button Content="Command 1" Margin="5" Command="{Binding ClickCommand}" CommandParameter="1"/>
    <Button Content="Command 2" Margin="5" Command="{Binding ClickCommand}" CommandParameter="2"/>
    <Button Content="Command 3" Margin="5" Command="{Binding ClickCommand}" CommandParameter="3"/>
</StackPanel>

MainViewModel:

public class ViewModel : BindableBase
{
    public InteractionRequest<Confirmation> StretchModeRequest1 { get; private set; }
    public InteractionRequest<Confirmation> StretchModeRequest2 { get; private set; }
    public InteractionRequest<Confirmation> StretchModeRequest3 { get; private set; }
    public ICommand ClickCommand { get; private set; }
    public ViewModel()
    {
        StretchModeRequest1 = new InteractionRequest<Confirmation>();
        StretchModeRequest2 = new InteractionRequest<Confirmation>();
        StretchModeRequest3 = new InteractionRequest<Confirmation>();
        ClickCommand = new DelegateCommand<object>(OnClicked);
    }
    private void OnClicked(object obj)
    {
        int index = Convert.ToInt32(obj);
        var confirmation = new Confirmation() { Content = "Test", Title = string.Format("Bla {0}", index) };
        switch (index)
        {
            case 1: StretchModeRequest1.Raise(confirmation); break;
            case 2: StretchModeRequest2.Raise(confirmation); break;
            case 3: StretchModeRequest3.Raise(confirmation); break;
            default:
                break;
        }
    }
}

TestControl:

<UserControl x:Class="WpfApp1.TestControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApp1"
         xmlns:vm="clr-namespace:WpfApp1.ViewModels">
<UserControl.Resources>
    <ResourceDictionary>
        <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter"/>
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
    <vm:TestViewModel/>
</UserControl.DataContext>
<StackPanel Margin="3,3,3,0">
    <RadioButton Margin="2" GroupName="Mode" Content="None" IsChecked="{Binding SelectedMode, 
        diag:PresentationTraceSources.TraceLevel=High, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.None}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <RadioButton Margin="2" GroupName="Mode" Content="Limits" IsChecked="{Binding SelectedMode, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.Limits}, Mode=TwoWay}"/>
    <RadioButton Margin="2" GroupName="Mode" Content="Static" IsChecked="{Binding SelectedMode, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.Static}, Mode=TwoWay}"/>
</StackPanel>

TestViewModel:

public class TestViewModel : BindableBase
{
    private StretchType _stretchMode;
    public TestViewModel()
    {
        SelectedMode = StretchType.None;
    }
    public StretchType SelectedMode
    {
        get { return _stretchMode; }
        set { SetProperty(ref _stretchMode, value); }
    }
}

コンバータ:

public class EnumToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(parameter);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value) ? parameter : Binding.DoNothing;
    }
}

回答 1 件
  • 一意の GroupName を使用する必要があります  各ビューに対して、例えば:

    <RadioButton Margin="2" GroupName="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" Content="None" IsChecked="{Binding SelectedMode, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.None}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <RadioButton Margin="2" GroupName="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" Content="Limits" IsChecked="{Binding SelectedMode, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.Limits}, Mode=TwoWay}"/>
    <RadioButton Margin="2" GroupName="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" Content="Static" IsChecked="{Binding SelectedMode, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static vm:StretchType.Static}, Mode=TwoWay}"/>
    
    

あなたの答え