UWP “form”验证事件

huangapple go评论132阅读模式
英文:

UWP "form" validation event

问题

我有一个包含基本输入控件的 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>
英文:

I have a StackPanel of basic input controls. Some combinations selections are invalid. I would like to recompute the "form"s validation every time one of the inputs change. Is there some kind of event you can listen to on the parent control that will trigger when ever an observable property changes on a child?

&lt;StackPanel HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;
    &lt;RadioButtons Header=&quot;Options:&quot;&gt;
        &lt;RadioButton Content=&quot;Option 1&quot;/&gt;
        &lt;RadioButton Content=&quot;Option 2&quot;/&gt;
    &lt;/RadioButtons&gt;
    &lt;ToggleSwitch OnContent=&quot;On&quot; OffContent=&quot;Off&quot;/&gt;
    &lt;ComboBox&gt;
        &lt;ComboBoxItem Content=&quot;Item 1&quot;/&gt;
        &lt;ComboBoxItem Content=&quot;Item 2&quot;/&gt;
    &lt;/ComboBox&gt;
&lt;/StackPanel&gt;

答案1

得分: 1

建议使用数据绑定来监听控件的选择变化。在数据绑定中,使用 Binding.Mode 设置为 TwoWay 的绑定,目标的变化将自动传播到源。

定义一个类,继承自 INotifyPropertyChanged,并在 PropertyChanged 事件中重新计算“表单”的验证。

Page.xaml

&lt;Grid&gt;
    &lt;StackPanel HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot; &gt;
        &lt;controls:RadioButtons Header=&quot;选项:&quot; SelectedIndex=&quot;{x:Bind selectItems.RadioButtonsIndex, Mode=TwoWay}&quot;&gt;
            &lt;RadioButton Content=&quot;选项 1&quot;/&gt;
            &lt;RadioButton Content=&quot;选项 2&quot;/&gt;
        &lt;/controls:RadioButtons&gt;
        &lt;ToggleSwitch OnContent=&quot;打开&quot; OffContent=&quot;关闭&quot; IsOn=&quot;{x:Bind selectItems.ToggleSwitchState, Mode=TwoWay}&quot;/&gt;
        &lt;ComboBox SelectedIndex=&quot;{x:Bind selectItems.ComboBoxSelectIndex, Mode=TwoWay}&quot;&gt;
            &lt;ComboBoxItem Content=&quot;项目 1&quot;/&gt;
            &lt;ComboBoxItem Content=&quot;项目 2&quot;/&gt;
        &lt;/ComboBox&gt;
    &lt;/StackPanel&gt;
&lt;/Grid&gt;

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 
            &amp;&amp; selectItems.ToggleSwitchState == true 
            &amp;&amp; selectItems.ComboBoxSelectIndex == 1) 
        {
            Debug.WriteLine(&quot;无效的组合选择&quot;);
        }
    }
}

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 =&gt; _radioButtonsIndex;
        set
        {
            _radioButtonsIndex = value;
            NotifyPropertyChanged();
        }
    }

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

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

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = &quot;&quot;)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
英文:

It is recommended to use Data Binding to listen the selection change of the controls. In Data Binding, use Binding.Mode TwoWay bindings, changes to the target will automatically propagate to the source.

Define a class which inherits INotifyPropertyChanged, and recompute the "form"s validation in the PropertyChanged event.

Page.xaml

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

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 
            &amp;&amp; selectItems.ToggleSwitchState == true 
            &amp;&amp; selectItems.ComboBoxSelectIndex == 1) 
        {
            Debug.WriteLine(&quot;Invalid combinations selections&quot;);
        }
    }
}

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 =&gt; _radioButtonsIndex;
        set
        {
            _radioButtonsIndex = value;
            NotifyPropertyChanged();
        }
    }

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

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

    public event PropertyChangedEventHandler PropertyChanged;

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

答案2

得分: 0

我猜最好的做法是在每个子控件上注册处理程序,它们都引发相同的事件。

<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]" });
}

然后你可以监听这个事件。

英文:

I guess the best thing to do is to register handlers on each child control which all raise the same event.

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

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

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

Then you can listen to this event.

huangapple
  • 本文由 发表于 2023年6月9日 12:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定