Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何通过XAML绑定值_.net_Wpf_Xaml_Data Binding - Fatal编程技术网

.net 如何通过XAML绑定值

.net 如何通过XAML绑定值,.net,wpf,xaml,data-binding,.net,Wpf,Xaml,Data Binding,我正在将DataTable值绑定到ObservableCollection,并尝试将这些项绑定到ListView 当我通过XAML进行绑定时,比如:ItemsSource=“{binding Collection}”,它不会显示值,但当我使用C#code绑定值时,它会正确显示值 我找不到这种行为的原因。请提出一个解决方案 在C#代码中: // Declaration of the Observable Collection item. ObservableCollection<DataTa

我正在将DataTable值绑定到ObservableCollection,并尝试将这些项绑定到ListView

当我通过XAML进行绑定时,比如:
ItemsSource=“{binding Collection}”
,它不会显示值,但当我使用C#code绑定值时,它会正确显示值

我找不到这种行为的原因。请提出一个解决方案

在C#代码中:

// Declaration of the Observable Collection item.
ObservableCollection<DataTable> _observableCollection = new ObservableCollection<DataTable>();

public ObservableCollection<DataTable> Collection
{
    get { return _observableCollection; }
}
lstVw.ItemsSource = Collection;
<Grid>
   <ListView Name="lstVw" ItemsSource="{Binding Path=Collection}" Height="auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
       <ListView.View>
          <GridView>
               <GridViewColumn  Header="OrderID" Width="auto" >
                   <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Tag="{Binding OrderID}" Content="{Binding OrderID}"/>
                        </DataTemplate>
                   </GridViewColumn.CellTemplate>
               </GridViewColumn>
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding CustomerID}" Header="CustomerID" />
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding ProductID}" Header="ProductID" />
          </GridView>
      </ListView.View>
   </ListView>
</Grid>
在XAML中:

// Declaration of the Observable Collection item.
ObservableCollection<DataTable> _observableCollection = new ObservableCollection<DataTable>();

public ObservableCollection<DataTable> Collection
{
    get { return _observableCollection; }
}
lstVw.ItemsSource = Collection;
<Grid>
   <ListView Name="lstVw" ItemsSource="{Binding Path=Collection}" Height="auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
       <ListView.View>
          <GridView>
               <GridViewColumn  Header="OrderID" Width="auto" >
                   <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Tag="{Binding OrderID}" Content="{Binding OrderID}"/>
                        </DataTemplate>
                   </GridViewColumn.CellTemplate>
               </GridViewColumn>
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding CustomerID}" Header="CustomerID" />
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding ProductID}" Header="ProductID" />
          </GridView>
      </ListView.View>
   </ListView>
</Grid>

解决此问题有两个选项,首先将DataContext设置为窗口或用户控件,然后在绑定处使用元素名称

这是第二个解决方案

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication6.MainWindow"
    x:Name="myWindow"
    Title="MainWindow"
    Width="640" Height="480">

  <Grid>
   <ListView Name="lstVw" ItemsSource="{Binding Path=Collection, ElementName=myWindow}" Height="auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
       <ListView.View>
          <GridView>
               <GridViewColumn  Header="OrderID" Width="auto" >
                   <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Tag="{Binding OrderID}" Content="{Binding OrderID}"/>
                        </DataTemplate>
                   </GridViewColumn.CellTemplate>
               </GridViewColumn>
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding CustomerID}" Header="CustomerID" />
               <GridViewColumn Width="auto" DisplayMemberBinding="{Binding ProductID}" Header="ProductID" />
          </GridView>
      </ListView.View>
   </ListView>
  </Grid>

</Window>


希望这有帮助

可能数据上下文不正确。您在哪里/如何设置窗口或控件的数据上下文?事实上,我没有得到您的消息,先生。在这里,如果DataContextYour集合位于代码隐藏中,但必须为XAML设置数据上下文,我将使用ItemMsource选项,例如,在代码中:RootGrid.DataContext=this;