从wpf中的列表视图中的文本框获取值 [英] get values from textboxes in listview in wpf

查看:121
本文介绍了从wpf中的列表视图中的文本框获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要从两个文本框中获取时间值,并且需要将该文本框值的差值设置为另一个文本框.

我已使用此XAML代码

Hi All,

I need to get time values from two textboxes and need to set difference value of that text boxes value to another text box.

I have used this XAML code

<ListView x:Name="listV_EmpAttendence" ItemsSource="{Binding}"><br />
                        <ListView.ItemContainerStyle><br />
                            <Style TargetType="ListViewItem"><br />
                                <EventSetter Event="GotFocus" Handler="Item_Got_Focus_listAttendence"/><br />
                            </Style><br />
                        </ListView.ItemContainerStyle><br />
                        <ListView.View><br />
                            <GridView>       <br />
                                <GridViewColumn Header="In Time"><br />
                                    <GridViewColumn.CellTemplate><br />
                                        <DataTemplate><br />
                                            <TextBox x:Name="txt_InTime" <br />
                                                     Text="{Binding Path=InTime, StringFormat=t,ValidatesOnDataErrors=True}"                                                     <br />
                                                     MinWidth="100"<br />
                                                     /><br />
                                        </DataTemplate><br />
                                    </GridViewColumn.CellTemplate><br />
                                </GridViewColumn><br />
                                <GridViewColumn Header="Out Time"><br />
                                    <GridViewColumn.CellTemplate><br />
                                        <DataTemplate><br />
                                            <TextBox Text="{Binding Path=OutTime, StringFormat=t,ValidatesOnDataErrors=True}"<br />
                                                     x:Name="txt_OutTime" MinWidth="100" /><br />
                                        </DataTemplate><br />
                                    </GridViewColumn.CellTemplate><br />
                                </GridViewColumn><br />
                                <GridViewColumn Header="No of Hours"><br />
                                    <GridViewColumn.CellTemplate><br />
                                        <DataTemplate><br />
                                            <TextBox x:Name="txt_Hours" Text="{Binding Path=NoOfHours}" MinWidth="100" LostFocus="txt_Hours_LostFocus"/><br />
                                        </DataTemplate><br />
                                    </GridViewColumn.CellTemplate><br />
                                </GridViewColumn>                                <br />
                            </GridView><br />
                        </ListView.View><br />
                    </ListView>



差异应为:
txt_Hours = txt_OutTime-txt_InTime

我该怎么做.如果有人有任何想法,请告诉我.

谢谢,
Chamara



Difference should get as:
txt_Hours=txt_OutTime-txt_InTime

How can I do it. If someone has any idea please let me know.

Thanks,
Chamara

推荐答案

我可以找到解决此问题的方法.

步骤01

在XAML中,
需要像这样向txt_InTime和txt_OutTime添加lostfocus事件,

I could find solution to this problem.

Step 01

In XAML,
Need to add lostfocus event to txt_InTime and txt_OutTime like this,

<textbox x:name="txt_InTime" text="{Binding Path=InTime,StringFormat=t,ValidatesOnDataErrors=True}" lostfocus="txt_InTime_LostFocus" xmlns:x="#unknown" />





<textbox name="txt_OutTime" text="{Binding Path=OutTime,<br mode=" hold=" />StringFormat=t,ValidatesOnDataErrors=True}" lostfocus="txt_OutTime_LostFocus" />



然后需要将gotfocus事件添加到txt_Hours.同样在文本绑定中,将updateSourceTriger设置为Explicit,如下所示.



Then Need to add gotfocus event to txt_Hours.Also in in Text binding,set updateSourceTriger to Explicit like this.

<textbox x:name="txt_Hours" text="{Binding Path=NoOfHours,UpdateSourceTrigger=Explicit}" lostfocus="txt_Hours_LostFocus" xmlns:x="#unknown" />



步骤02
在C#文件中,



Step 02
In C# file,

//Get value from txt_InTime cell in ListView
        private void txt_InTime_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                //Get cell value by using sender Object
                string inTime = ((TextBox)sender).Text;
                //_InTime is TimeSpan Parameter
                _InTime = Convert.ToDateTime(inTime).TimeOfDay;
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid Time Format");
            }
        }
//Get value from txt_OutTime cell in ListView
        private void txt_OutTime_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                //Get cell value by using sender Object
                string outTime = ((TextBox)sender).Text;
                _OutTime = Convert.ToDateTime(outTime).TimeOfDay;
            }
            catch (Exception)
            {
                
                MessageBox.Show("Invalid Time Format");
            }
        }



在txt_Hours的GotFocuss事件中,可以像这样在listView的选定行中将不同的Time分配给txt_Hours单元格,



In GotFocuss event of txt_Hours,can assign different of Time into txt_Hours cell in selected row in listView like this,

// When focuss to txt_Hours cell in selected row in listView difference of InTime and outTime
        // set to this cell
        private void txt_Hours_GotFocus(object sender, RoutedEventArgs e)
        {
            //Calculate Time Difference. Difference represent in decimal format
            if (_OutTime != null && _InTime != null)
            {
                if (_OutTime > _InTime)
                {
                    _NoofHours = _OutTime.Subtract(_InTime).TotalHours.ToString();
                }
                else
                {
                    _NoofHours = (new TimeSpan(24, 0, 0) - _InTime.Subtract(_OutTime)).TotalHours.ToString();
                }
                 //Set Difference of Time Value to txt_Hours
                ((TextBox)sender).SetCurrentValue(TextBox.TextProperty, _NoofHours);
            }
            else
            {
                MessageBox.Show("Invalid value of InTime or OutTime");
            }
        }



冷静
谢谢大家Every



Be Cool
Thank U Every One


这篇关于从wpf中的列表视图中的文本框获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆