我正在開發一個execise應用程序。我的應用程序採用MVVM。我正在嘗試創建一個包含兩個用戶控件的窗口,其中一個用戶控件包含一個從viewmodel獲取其數據的datagrid。
我希望datagrid在應用運行時自動填充我的默認值(私有文件)。但是存在綁定錯誤: System.Windows.Data錯誤:4:找不到與參考'ElementName = windowView'綁定的源。 BindingExpression:路徑= ActivePacket;的DataItem = NULL;目標元素是'DataGrid'(Name ='dataGrid1');目標屬性是'ItemsSource'(類型'IEnumerable')UserControl中的數據綁定問題
謝謝!
這裏是我的代碼:
========= MainWindow.xaml ================
<Window x:Class="Project.Abc.Try.MainWindow"
x:Name="windowView"
xmlns:local="clr-namespace:Project.Abc.Try"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="600">
<Window.DataContext>
<local:PayloadViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="78*" />
<RowDefinition Height="233*" />
</Grid.RowDefinitions>
<local:CmdMenuView Grid.Row="0" Margin="6,6,3,6" />
<local:PayloadView Grid.Row="1" Margin="6,6,3,6" />
</Grid>
</Window>
== ========== PayloadView.xaml ===========
<UserControl x:Class="Project.Abc.Try.PayloadView"
x:Name="PLview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="199" d:DesignWidth="588" >
<Grid Height="200" Width="580" >
<!--<DataGrid AutoGenerateColumns="False" Height="45" HorizontalAlignment="Left" Margin="36,20,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="500" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActivePacket}"> -->
<DataGrid AutoGenerateColumns="False" Height="45" HorizontalAlignment="Left" Margin="36,20,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="500" ItemsSource="{Binding ElementName=windowView, Path=ActivePacket}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PacketId, Mode=TwoWay}" Header="PacketID " Width="*" />
<DataGridTextColumn Binding="{Binding PacketLength, Mode=TwoWay}" Header="PacketLength" Width="*" />
<DataGridTextColumn Binding="{Binding Spare}" Header="Byte 6" Width="*" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Send" Command="{Binding Path=SendCommand}" Height="23" HorizontalAlignment="Left" Margin="310,122,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
</UserControl>
=================== PayloadViewModel.cs ========================
namespace Project.Abc.Try
{
public class PayloadViewModel : ObservableObject
{
// ......
private CmdPacket _activePacket;
public CmdPacket ActivePacket
{
get { return _activePacket; }
set
{
if (value != _activePacket)
{
_activePacket = value;
OnPropertyChanged("ActivePacket");
}
}
}
// .........
}
謝謝,我根據你的建議修改了我的代碼。綁定錯誤消息已經消失,但我仍然看不到任何數據顯示在UI上的DataGrid中。調試 - 輸出窗口總是有「System.Windows.Data信息:10:不能使用綁定檢索值,並且不存在有效的回退值;使用默認值而不是BindingExpression:Path = ActivePacket; DataItem = null;目標元素是'DataGrid'( Name ='dataGrid1');目標屬性是'ItemsSource'(類型'IEnumerable') 「。有什麼問題? –
@KaiMon CmdPacket是集合類型嗎?是否初始化?請在http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource.aspx上閱讀有關'DataGrid.ItemsSource'的更多信息,以幫助您解決這個問題。 –
謝謝。我發現程序沒有創建CmdPacket對象,因爲它的構造器沒有正確設置。 –