XAML UWP“表单”验证事件

qnyhuwrf  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(106)

我有一个StackPanel的基本输入控件。某些组合选择无效。我想在每次输入发生变化时重新计算“表单”的验证。是否有某种事件可以在父控件上侦听,以便在子控件上的可观察属性发生更改时触发?

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <RadioButtons Header="Options:">
        <RadioButton Content="Option 1"/>
        <RadioButton Content="Option 2"/>
    </RadioButtons>
    <ToggleSwitch OnContent="On" OffContent="Off"/>
    <ComboBox>
        <ComboBoxItem Content="Item 1"/>
        <ComboBoxItem Content="Item 2"/>
    </ComboBox>
</StackPanel>
ffvjumwh

ffvjumwh1#

建议使用数据绑定来侦听控件的选择更改。在数据绑定中,使用Binding.ModeTwoWay绑定,对目标的更改将自动传播到源。
定义一个继承INotifyPropertyChanged的类,并在PropertyChanged事件中重新计算“表单”的验证。

Page.xaml

<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
        <controls:RadioButtons Header="Options:" SelectedIndex="{x:Bind selectItems.RadioButtonsIndex, Mode=TwoWay}">
            <RadioButton Content="Option 1"/>
            <RadioButton Content="Option 2"/>
        </controls:RadioButtons>
        <ToggleSwitch OnContent="On" OffContent="Off" IsOn="{x:Bind selectItems.ToggleSwitchState, Mode=TwoWay}"/>
        <ComboBox SelectedIndex="{x:Bind selectItems.ComboBoxSelectIndex, Mode=TwoWay}">
            <ComboBoxItem Content="Item 1"/>
            <ComboBoxItem Content="Item 2"/>
        </ComboBox>
    </StackPanel>
</Grid>

Page.xaml.cs

public sealed partial class MainPage : Page
{
    public SelectItems selectItems = new SelectItems();
    public MainPage()
    {
        this.InitializeComponent();
        selectItems.PropertyChanged += SelectItems_PropertyChanged;
    }

    private void SelectItems_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (selectItems.RadioButtonsIndex == 1 
            && selectItems.ToggleSwitchState == true 
            && selectItems.ComboBoxSelectIndex == 1) 
        {
            Debug.WriteLine("Invalid combinations selections");
        }
    }
}

public class SelectItems: INotifyPropertyChanged
{
    private int _radioButtonsIndex;
    private bool _toggleSwitchState;
    private int _comboBoxSelectIndex;

    public SelectItems()
    {
        RadioButtonsIndex = 0;
        ToggleSwitchState= false; 
        ComboBoxSelectIndex= 0;
    }
    public int RadioButtonsIndex
    {
        get => _radioButtonsIndex;
        set
        {
            _radioButtonsIndex = value;
            NotifyPropertyChanged();
        }
    }

    public bool ToggleSwitchState
    {
        get => _toggleSwitchState;
        set
        {
            _toggleSwitchState = value;
            NotifyPropertyChanged();
        }
    }

    public int ComboBoxSelectIndex
    {
        get => _comboBoxSelectIndex;
        set
        {
            _comboBoxSelectIndex = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
3yhwsihp

3yhwsihp2#

我想最好的办法是在每个子控件上注册处理程序,这些处理程序都引发相同的事件。

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
    <RadioButtons Header="Options:" SelectionChanged="SelectionChanged">
        <RadioButton Content="Option 1"/>
        <RadioButton Content="Option 2"/>
    </RadioButtons>
    <ToggleSwitch x:Name="MyToggle" Toggled="MyToggle_Toggled" OnContent="On" OffContent="Off"/>
    <ComboBox SelectionChanged="SelectionChanged">
        <ComboBoxItem Content="Item 1"/>
        <ComboBoxItem Content="Item 2"/>
    </ComboBox>
</StackPanel>
winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;

void winrt::App1::implementation::MainWindow::MyToggle_Toggled(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    m_propertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"[PropertyName]" });
}

void winrt::App1::implementation::MainWindow::SelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e)
{
    m_propertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"[PropertyName]" });
}

然后你可以听这个事件。

相关问题