延迟触发,用于数据绑定计算

时间:2018-12-08 21:33:26

标签: c# wpf data-binding triggers calculated-field

我是WPF的新手,目前正在学习数据绑定的概念。

我的简化XAML代码。除了我的问题(在下面)之外,它还可以正常工作-通过GUI快速而肮脏地放置对象,将在工作后被清除:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
        </Grid>
        <Grid Grid.Row="1">
            <GroupBox Header="Change Type:" Height="95" Width="100" VerticalAlignment="Top" Margin="270,4,422,0" >
                <StackPanel>
                    <RadioButton x:Name="RbtAdd" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
                        <WrapPanel>
                            <TextBlock Text="Add" Foreground="Green"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton x:Name="RbtPull" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
                        <WrapPanel>
                            <TextBlock Text="Pull" Foreground="Blue"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton x:Name="RbtModify" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
                        <WrapPanel>
                            <TextBlock Text="Modify" Foreground="DarkGray"/>
                        </WrapPanel>
                    </RadioButton>
                </StackPanel>
            </GroupBox>

            <TextBlock x:Name="txtCurStock" HorizontalAlignment="Left" Margin="330,181,0,0" TextWrapping="Wrap" Text="{Binding Path=CurrentStock}" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" TextAlignment="Center"/>

            <Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
            <Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
            <Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
            <TextBlock Text="{Binding Path=NewStock}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
            <TextBox x:Name="txtComment"  HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
            <Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>

            <TextBlock x:Name ="txtModindicator" HorizontalAlignment="Left" Margin="433,181,0,0" TextWrapping="Wrap" Text="-" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>
            <TextBlock x:Name ="txtResindicator" HorizontalAlignment="Left" Margin="663,182,0,0" TextWrapping="Wrap" Text="=" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>

        </Grid>
    </Grid>

现在缩短的C#代码:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Runtime.CompilerServices;


namespace SomeWPF
{
    /// <summary>
    /// Interaction logic for ModifyWindow.xaml
    /// </summary>
    public partial class MainWindow : INotifyPropertyChanged
    {

        public enum Mymode
        {
            add,
            pull,
            modify
        }

        public Mymode mode;

        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();

            CurrentStock = 5;
            RbtPull.IsChecked = true;
            ModEntry = 1;

        }

        private void ModeRadio_Checked(object sender, RoutedEventArgs e)
        {
            if (sender != null)
            {
                if (sender.Equals(RbtAdd))
                {
                    mode = Mymode.add;
                    txtModindicator.Text = "+";
                    txtComment.Text = "Add";
                    lblOperation.Content = "Stock to Add:";
                }
                else if (sender.Equals(RbtPull))
                {
                    mode = Mymode.pull;
                    txtModindicator.Text = "-";
                    txtComment.Text = "Pull";
                    lblOperation.Content = "Stock to Pull:";
                }
                else
                {
                    mode = Mymode.modify;
                    txtModindicator.Text = "~";
                    lblOperation.Content = "Corrected Quantity:";
                    txtComment.Text = "Mod";
                }
                TxtEntry_TextChanged(sender, null);
            }
        }

        private void TxtEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mode == Mymode.add)
            {
                NewStock = CurrentStock + ModEntry;
            }
            else if (mode == Mymode.pull)
            {
                NewStock = CurrentStock - ModEntry;
            }
            else
            {
                NewStock = ModEntry;
            }

        }

        #region Binding Stuff

        private int _newstock;
        public int NewStock
        {

            get
            {

                return _newstock;
            }
            set
            {

                if (_newstock != value)
                {
                    _newstock = value;
                    OnPropertyChanged();
                }
            }
        }

        private int _modentry;
        public int ModEntry
        {
            get
            {
                return _modentry;
            }
            set
            {
                if (_modentry != value)
                {
                    _modentry = value;
                    OnPropertyChanged();
                }
            }
        }

        private int _currentstock;
        public int CurrentStock
        {
            get
            {
                return _currentstock;
            }
            set
            {
                if (_currentstock != value)
                {
                    _currentstock = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion


    }
}

因此,此窗口是小程序中用于存货存储的弹出窗口,供用户输入存货的移动。 到目前为止,一切工作正常,我现在想做一个非常简单的计算部分。使用“旧” winforms c#,您只需“手动”获取值并更新结果的text属性,但是我们(I)当然想学习新东西,并进行数据绑定。 该代码也可以进行计算,但是触发器不是我想要的。

假设当前库存为5 当窗口加载时,通过代码将模式设置为Pull(RbtPull),并将用户条目(绑定到ModEntry)设置为1。因此,NewStock应该为4,可以正确显示。 (好) 另外,注释字段(目前用于调试)显示ModEntry值1。 到目前为止一切都很好。

现在,我在“要提取的库存”字段中输入3,但是什么也没有发生。 (我希望它能够“实时”做出反应)。新库存仍显示为4,注释仍显示为1。 当我离开该字段(并单击进入注释字段)时-检测到属性更改,并且注释字段也显示3(= ModEntry)-因此它不是“实时”的,而是仅在该字段失去焦点时才触发,但这会触发也可以接受。

真正的问题是:新库存保持4,并且不计算。 现在,当我再次输入“要拉动的库存”字段并将值更改为5时,“新库存”字段将更新为2(因此更改为我在5-3 = 2之前输入的值) 再用5覆盖该字段会将新的Stock更改为0。 因此,它总是“落后一步”。

从我发现的内容中,我有一个想法,我需要某种Binding Converter而不是我的计算方法,但是我找不到真正合适的东西,并且对数据绑定还不够熟悉。已经直接在绑定变量代码中尝试了一些方法,但是没有任何效果。如果有人能向我暗示正确的方向,我将非常感激。 (不需要镀银的解决方案,而只是想知道哪种搜索方式(例如,我使用的那种装订方法是否完全有意义,或者是否有必要添加等)。

非常感谢!

PS:当然,如果有人有动力提供镀银解决方案,我也将不胜感激。 :)-对不起,英语不好,没有母语的人。

1 个答案:

答案 0 :(得分:0)

@nosale的第二个链接(请参阅评论)提供了问题的答案。 将XAML字段txtEntry和Result字段都设置为UpdateSourceTrigger = PropertyChanged可以解决此问题。

所以正确的块现在看起来像这样,而无需更改c#代码:

    <Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
    <Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
    <Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
    <TextBlock Text="{Binding Path=NewStock, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
    <TextBox x:Name="txtComment"  HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
    <Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>

原因是,文本框具有默认UpdateSourceTrigger = LostFocus而不是PropertyChanged,以防止用户输入错字进行更新。

一些新知识:WPF很酷,可以自动处理不合理的值(例如null或字符串),并将字段标记为红色! :)

再次感谢您的链接!