WPF中的列表框中未显示的值 [英] values not displayed in listbox in WPF

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

问题描述


我在WPF中创建了一个简单的应用程序,但是面临一个问题.问题是我创建了两个页面,即Homepage.xaml和ExpenseReportPage.xaml. Homepage.xaml中有一个列表框,其中一些用户名显示为与XML绑定.当用户从列表框中选择任何用户时,ExpenseReportPage.xaml将打开,有关该选定用户的所有详细信息.详细信息显示在ExpenseReportPage.xaml的列表框中.第二页上有两个列表框.但是,其中一个显示详细信息,而另一个不显示.请帮我.我已逐页使用此代码:.....

HomePage.xaml:

Hi,
I have a created a simple application in WPF.but, there is a problem that is facing. Problem is that i have created two pages i.e. Homepage.xaml and ExpenseReportPage.xaml. There is a listbox in Homepage.xaml in which some users names are displayed binding with XML. and when user select any user from listbox, ExpenseReportPage.xaml opens and all details about that selected user. Details are displayed in listbox on ExpenseReportPage.xaml. There are two listboxes on second page. but, one of them displays details, another not displays. Please help me. I have used this code page by page:.....

HomePage.xaml:

<grid name="Grid1">
        <grid.resources>
            
            <!--Expense Report Data-->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:xdata xmlns:x="#unknown">
                    <expenses xmlns="">
                        <person name="Rahul" department="IT">
                            <expense expensetype="Lunch" expenseamount="40" />
                            <expense expensetype="Transportation" expenseamount="100" />
                            <expense expensetype="BrakeFast" expenseamount="30" />
                            <expense expensetype="Entertainment" expenseamount="150" />
                        </person>
                        <person name="Mannu" department="IT">
                            <expense expensetype="Lunch" expenseamount="30" />
                            <expense expensetype="Transportation" expenseamount="50" />
                            <expense expensetype="BrakeFast" expenseamount="10" />
                            <expense expensetype="Entertainment" expenseamount="100" />
                        </person>
                        <person name="Madhu" department="Computers">
                            <expense expensetype="Lunch" expenseamount="50" />
                            <expense expensetype="Transportation" expenseamount="70" />
                            <expense expensetype="BrakeFast" expenseamount="20" />
                            <expense expensetype="Entertainment" expenseamount="70" />
                        </person>
                        <person name="Abhishek" department="Management">
                            <expense expensetype="Lunch" expenseamount="50" />
                            <expense expensetype="Transportation" expenseamount="60" />
                            <expense expensetype="BrakeFast" expenseamount="25" />
                            <expense expensetype="Entertainment" expenseamount="200" />
                        </person>
                        <person name="Jai" department="Maths">
                            <expense expensetype="Lunch" expenseamount="45" />
                            <expense expensetype="Transportation" expenseamount="170" />
                            <expense expensetype="BrakeFast" expenseamount="40" />
                            <expense expensetype="Entertainment" expenseamount="250" />
                        </person>
                    </expenses>
                </x:xdata>
            </XmlDataProvider>
            
            <!--Name Item Template-->
            <datatemplate x:key="nameItemTemplate" xmlns:x="#unknown">
                <Label Content="{Binding XPath=@Name}" ></Label>
            </datatemplate>
            
        </grid.resources>
        
        <grid.columndefinitions>
            <columndefinition width="294" />
            <columndefinition />
        </grid.columndefinitions>
        <grid.rowdefinitions>
            <rowdefinition height="Auto" />
            <rowdefinition />
        </grid.rowdefinitions>
        
        <dockpanel grid.column="0" grid.row="0" grid.columnspan="2">
            <canvas dockpanel.dock="Left" width="230" height="100"></canvas>
        </dockpanel>
        
        <Label FontFamily="Verdana" FontSize="18" FontWeight="Bold" Foreground="#FF0066CC" Height="32" Margin="67,23,64,0" VerticalAlignment="Top" Grid.ColumnSpan="2">View Expense Report</Label>
        
        <grid margin="10" grid.column="0" grid.row="2">
            <grid.columndefinitions>
                <columndefinition />       
            </grid.columndefinitions>
            <grid.rowdefinitions>
                <rowdefinition height="Auto" />
                <rowdefinition />
                <rowdefinition height="Auto" />
            </grid.rowdefinitions>
            
            <!--People List-->
            <border grid.column="0" grid.row="0" height="35" padding="5" removed="#4E87D4">
                <Label VerticalAlignment="Center" Foreground="White">NAMES</Label>
            </border>
            <listbox name="peopleListBox" grid.column="0" grid.row="1">
                     ItemsSource="{Binding Source={StaticResource ExpenseDataSource},XPath=Person}" 
                     ItemTemplate="{StaticResource nameItemTemplate}">                
            </listbox>br mode="hold" />H>
                        
            <Button Name="viewdetails" Grid.Column="0" Margin="0,10,0,0" Grid.Row="2" Height="25" Width="130" Background="DarkOliveGreen" Foreground="White" FontSize="14" Click="view_Click" >VIEW</Button>
        </grid>    
    </grid>



HomePage.Xaml.cs:



HomePage.Xaml.cs:

public void view_Click(object sender, EventArgs e)
        {
            ExpenseReportPage objExp = new ExpenseReportPage(this.peopleListBox.SelectedItem);
            objExp.Show();
            this.Hide();
        }



ExpenseReportPage.xaml:



ExpenseReportPage.xaml:

<grid>
        <grid.background>
            <ImageBrush ImageSource="sky.jpg"></ImageBrush>
        </grid.background>
        <grid.columndefinitions>
            <columndefinition width="90*" />
            <columndefinition width="172*" />
        </grid.columndefinitions>
        <grid.rowdefinitions>
            <rowdefinition height="100" />
            <rowdefinition />
        </grid.rowdefinitions>
        <stackpanel grid.column="1" grid.row="0" height="25" width="Auto">
            <Label FontWeight="Bold" Foreground="Teal" FontSize="15">Expense Report For:</Label>
        </stackpanel>
        <grid grid.column="1" grid.row="1">
            
            <grid.columndefinitions>
                <columndefinition />
                <columndefinition />
            </grid.columndefinitions>
            <grid.rowdefinitions>                
                <rowdefinition height="25" />
                <rowdefinition height="15" />
                <rowdefinition height="10*" />
                <rowdefinition height="106*" />
            </grid.rowdefinitions>
                        
            <Label Foreground="Teal" FontWeight="Bold" FontSize="12" Width="50" HorizontalAlignment="Left">Name:</Label>
            <Label Content="{Binding XPath=@Name}" Foreground="Teal" FontWeight="Bold" FontSize="12" Margin="92,0,76.95,0" Grid.ColumnSpan="2"></Label>
            
            <Label Grid.Row="1" Foreground="Teal" Grid.ColumnSpan="2" FontWeight="Bold" FontSize="12" HorizontalAlignment="Left" Grid.RowSpan="2">Department:</Label>
            <Label Content="{Binding XPath=@Department}" Grid.Row="1" Foreground="Teal" Grid.ColumnSpan="2" FontWeight="Bold" FontSize="12" Grid.RowSpan="2" Margin="92,0,76.95,0"></Label>
            
            <grid grid.row="3" margin="0,0,5,5">
                <grid.resources>
                    <datatemplate x:key="typeItemTemplate" xmlns:x="#unknown">
                        <Label Content="{Binding XPath=@ExpenseType}"/>                        
                    </datatemplate>                    
                </grid.resources>                
                
                <grid.columndefinitions>
                    <columndefinition />
                    <columndefinition />
                </grid.columndefinitions>
                <grid.rowdefinitions>
                    <rowdefinition height="28" />
                    <rowdefinition />
                </grid.rowdefinitions>
                
                <border removed="#4E87D4" grid.row="0" grid.columnspan="2" grid.column="0">
                    <Label FontWeight="Bold" Foreground="White" FontSize="12">Expense Type:</Label>
                </border>
                <listbox grid.column="0" grid.row="1" grid.columnspan="2" itemssource="{Binding XPath=Expense}" itemtemplate="{StaticResource typeItemTemplate}"></listbox>
            </grid>
            <grid grid.column="1" grid.row="3" margin="3,0,0,5">
                <grid.resources>
                    <datatemplate x:key="amountItemTemplate" xmlns:x="#unknown">
                        <Label Content="{Binding XPath=@ExpenseAmount}"/>
                    </datatemplate>
                </grid.resources>
                
                <grid.columndefinitions>
                    <columndefinition />
                    <columndefinition />
                </grid.columndefinitions>
                <grid.rowdefinitions>
                    <rowdefinition height="28" />
                    <rowdefinition />
                </grid.rowdefinitions>
                
                <border removed="#4E87D4" grid.row="0" grid.columnspan="2" grid.column="0">
                    <Label FontWeight="Bold" Foreground="White" FontSize="12">Amount:</Label>
                </border>
                <listbox grid.column="0" grid.row="1" grid.columnspan="2" itemssource="{Binding XPath=@Expense}" itemtemplate="{StaticResource amountItemTemplate}"></listbox>
            </grid>
        </grid>
    </grid>



ExpenseReportPage.Xaml.cs:



ExpenseReportPage.Xaml.cs:

public partial class ExpenseReportPage : Window
    {
        public ExpenseReportPage(object data)
        {
            InitializeComponent();
            this.DataContext = data;
        }
    }



please help me.



please help me.

推荐答案

actually in the second listbox, there is a binding problem. the correction is :

actually in the second listbox, there is a binding problem. the correction is :

" <listbox itemssource="{Binding XPath=Expense}" itemtemplate="{StaticResource amountItemTemplate}"></listbox> " 



Regards Rahul



Regards Rahul


这篇关于WPF中的列表框中未显示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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