数据绑定不起作用。 [英] Databinding not working.
问题描述
大家好,
我在WPF中使用了一个XML文件作为数据网格的数据源。
XML: -
Hi All,
I have used one XMLfile as data source for my datagrid in a WPF.
XML:-
<?xml version="1.0" encoding="utf-8" ?>
<Profiles>
<Profile Name="PGB570/120/600/120" Weight="19200" Bolt="42"/>
<Profile Name="PGB570/100/600/100" Weight="16400" Bolt="42"/>
<Profile Name="PGB570/70/600/70" Weight="13300" Bolt="36"/>
<Profile Name="PGB570/60/600/60" Weight="10300" Bolt="36"/>
<Profile Name="UC" Weight="8900" Bolt="30"/>
</Profiles>
在我的设计师中...
XAML:
And in my designer...
XAML:
<Window x:Class="TEST_APP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="TEST_APP" Height="280" Width="574""
ResizeMode="CanMinimize">
<Window.Resources>
<XmlDataProvider x:Key="ProfileData" Source="Resources/ProfileList.xml" XPath="/Profiles/Profile"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TabControl HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
<TabItem Header="Diagrid Diagonals">
<Grid>
<DataGrid x:Name="profileData" HorizontalAlignment="Center" Height="147" Margin="0,0,0,20" VerticalAlignment="Center" Width="283" DataContext="{StaticResource ProfileData}" ItemsSource="{Binding XPath=/Profiles/Profile}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Profile" Binding="{Binding XPath= @Name}"/>
<DataGridTextColumn Header="Max. Weight" Binding="{Binding XPath=@Weight}"/>
<DataGridTextColumn Header="Bolt Dia" Binding="{Binding XPath=@Bolt}"/>
</DataGrid.Columns>
</DataGrid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="303"/>
<ColumnDefinition Width="153"/>
<ColumnDefinition Width="91"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
设计器中的一切都很好用,我可以看到数据网格中填充了数据。但是在运行时它没有加载,只是一个空的数据网格。
请看看并帮助我。
我有什么尝试过:
代码在运行时也运行良好,但现在发生的事情并没有显示任何内容。这就是为什么我想知道!!。
我尝试过以不同的方式修改XAML但没有希望。请考虑我是WPF和XAML的初学者。
Everything works good in designer, and I can see the datagrid is populated with data. But on runtime its not loading, just an empty datagrid.
Please have a look and help me.
What I have tried:
The code worked very well on runtime also, but something happened now its not showing anything. That's why I am wondering!!.
I have tried modifying the XAML in different ways but no hope.Please consider as I am a begginer in WPF and XAML.
推荐答案
没有真正的解决方案,对不起..
但是:
我尝试了你的代码(复制到一个新项目),它按预期工作。所以我想说你的xaml或XML没有任何问题。但我认为最好将XML.file设置为build-action None并复制到输出目录,还是真的需要它作为资源?所以我会验证文件属性是否正确以及你想要什么。其他来自错误的文件设置我不知道可能会出错...可能重新开始(我只需要1分钟将您的代码和数据文件复制到新的解决方案),以排除一些项目设置你可能是无意中改变了......
No real solution, sorry..
But:
I tried your code (copied to a new project) and it worked as expected. So I would say there is nothing wrong with your xaml or XML. But I think it's best to set the XML.file to build-action None and copy to output directory or do you really need it as resource? So I would verify the file properties are correct and what you want. Other from wrong file Setting I have no idea would could have gone wrong with that... maybe start over again (I just needed 1 Minute to copy your code and data-file to a new solution), to rule out some project settings you may have changed unintentionally...
这篇关于数据绑定不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!