我有一个带有构造函数的类TabItemView.xaml.cs
。我将DataContext
设置为MainViewModel
因为在TabItemView中找到了DataGrid,我需要绑定数据。
public partial class TabItemView : UserControl
{
public TabItemView()
{
InitializeComponent();
MainViewModel myModel= new MainViewModel();
this.DataContext = myModel;
}
}
在课程MainWindow.xaml.cs
中,我还将DataContext
设置为button_Click事件中的MainViewModel
。此事件在按钮单击
public partial class MainWindow2 : Window
{
//Constructor and some other stuf
.....
private void AddInvoice_Click(object sender, RoutedEventArgs e)
{
count++;
string s = string.Format("Tab {0}", count);
mainViewModel.Items.Add(new ItemViewModel(s));
this.DataContext = mainViewModel;
}
}
这是我的MainViewModel
public class MainViewModel : ViewModelBase
{
private ObservableCollection<ItemViewModel> items = new ObservableCollection<ItemViewModel>();
private ObservableCollection<ItemViewModel> invoice_items = new ObservableCollection<ItemViewModel>();
public ObservableCollection<ItemViewModel> Invoice_Items
{
get
{
return invoice_items;
}
set
{
invoice_items = value;
OnPropertyChanged("Invoice_Items");
}
}
public ObservableCollection<ItemViewModel> Items
{
get
{
return items;
}
set
{
items = value;
OnPropertyChanged("Items");
}
}
private int _selectedTabIndex;
public int SelectedTabIndex
{
get { return _selectedTabIndex; }
set { _selectedTabIndex = value; }
}
public MainViewModel()
{
SelectedTabIndex = 0;
}
}
}
这是MainWindow
Grid>
<TabControl
ItemsSource="{Binding Items}" SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding TabName}"><TextBlock.Background><SolidColorBrush /></TextBlock.Background></TextBlock>
<Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}" BorderBrush="#00000000">
<Image Source="/WPF_AccApp;component/Images/11.gif" Height="11" Width="11"></Image>
</Button>
<DockPanel.Background>
<SolidColorBrush />
</DockPanel.Background>
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<views:TabItemView />
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
这适用于TabItemView
<UserControl x:Class="WPF_AccApp.Views.TabItemView"
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="640" d:DesignWidth="800" Focusable="True" IsHitTestVisible="True">
<Grid Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Top" MinWidth="768" MinHeight="446" Focusable="True">
<Grid.RowDefinitions>
<RowDefinition MinHeight="43" Height="*" />
<RowDefinition Height="*" MinHeight="45" />
<RowDefinition Height="*" MinHeight="45" />
<RowDefinition Height="*" MinHeight="350" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Supplier}" Grid.Column="1" Height="27" Name="textBox1" VerticalAlignment="Top" Margin="11,6,0,0" HorizontalAlignment="Stretch" Width="Auto" FontSize="14" HorizontalContentAlignment="Stretch" MinWidth="141" FlowDirection="LeftToRight" DataContext="{Binding}" />
<Label Content="Supplier" Height="27" Name="label2" VerticalAlignment="Top" FontSize="14" FontFamily="Tahoma" FontWeight="Bold" Margin="21,6,0,0" Width="Auto" IsEnabled="True" HorizontalAlignment="Stretch" Foreground="Black" Background="White" MinWidth="133" HorizontalContentAlignment="Stretch" />
<TextBox Text="{Binding SupplierBank}" Grid.Column="1" Grid.Row="1" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,0,0" Name="textBox11" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Row="1" Content="Supplier Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label3" VerticalAlignment="Top" Width="Auto" Background="White" MinWidth="133" />
<TextBox Text="{Binding SupplierAccount}" Grid.Column="1" Grid.Row="2" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,0,0" Name="textBox12" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Row="2" Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label4" VerticalAlignment="Top" Background="White" MinWidth="133" />
<TextBox Text="{Binding Buyer}" Grid.Column="4" FontSize="14" Height="27" Margin="11,6,20,0" Name="textBox2" VerticalAlignment="Top" HorizontalAlignment="Stretch" Width="Auto" MinWidth="141" />
<Label Grid.Column="3" Content="Buyer" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label5" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="White" MinWidth="133" />
<TextBox Text="{Binding BuyerBank}" Grid.Column="4" Grid.Row="1" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,20,0" Name="textBox3" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Column="3" Grid.Row="1" Content="Buyer Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label6" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Stretch" Background="White" MinWidth="133" />
<TextBox Text="{Binding BuyerAccount}" Grid.Column="4" Grid.Row="2" FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="11,6,20,0" Name="textBox4" VerticalAlignment="Top" Width="Auto" MinWidth="141" />
<Label Grid.Column="3" Grid.Row="2" Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,6,0,0" Name="label7" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Stretch" Background="White" MinWidth="133" />
<DataGrid AutoGenerateColumns="False" Grid.ColumnSpan="5" Grid.Row="3"
Height="104" HorizontalAlignment="Stretch" Margin="55,115,55,0" Name="dataGrid1"
VerticalAlignment="Top" Width="Auto" CanUserResizeRows="True" ItemsSource="{Binding Invoice_Items}"
IsReadOnly="False" SelectionUnit="Cell" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Item Name" Binding="{Binding Item_Name, Mode=TwoWay}"/>
<DataGridTextColumn Header="Mesure" Binding="{Binding Mesure, Mode=TwoWay}"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}"/>
<DataGridTextColumn Header="Price exc VAT" Binding="{Binding Price_exc_VAT, Mode=TwoWay}"/>
<DataGridTextColumn Header="VAT" Binding="{Binding VAT, Mode=TwoWay}"/>
<DataGridTextColumn Header="Price Inc VAT" Binding="{Binding Price_inc_VAT, Mode=TwoWay}"/>
<DataGridTextColumn Header="Total" Binding="{Binding Total, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
<TextBox Grid.ColumnSpan="3" Grid.Row="3" Height="27" HorizontalAlignment="Stretch" Name="textBox5" VerticalAlignment="Top" Grid.Column="1" Margin="11,0,0,0" />
<Label Background="White" Content="Shipping Address" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="21,0,0,0" MinWidth="133" Name="label1" VerticalAlignment="Top" Grid.Row="3" />
<Label Background="White" Content="Invoice #" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="78,65,0,0" MinWidth="133" Name="label8" VerticalAlignment="Top" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" />
</Grid>
现在问题。当我仅在MainWindow
中设置DataContext时,我的TextBoxes
正在做出正常反应,我的ObservableCollection
项会收到有关更改的通知。当我添加新标签时,它会在TextBoxes
中正常创建而没有文字。
但是,当我在MainWindow
和TabItemView
中设置DataContext时(就像我添加的代码一样)通知我的Invoice_Item ObservableCollection
有关DataGrid
我的DataContext中的更改两个ObservableCollection
都丢失了,他们不再收到有关更改的通知。当我添加新标签时,它会在TextBox
和DataGrid
中添加我之前标签中添加的数据。
此外,当我没有在MainWindow
中设置DataContext并仅在TabItemView
中设置ObservableCollection
时,Invoice_Items会收到有关DataGrid
更改的通知,但是当我添加新内容时选项卡,DataGrid
的数据只是在每个添加的标签中出现。
为什么会发生这种情况,我做错了什么?我该如何解决这个问题呢? 如果您需要更多代码或信息,请说。我正在寻找任何帮助,以了解如何解决我的问题。谢谢你的答案。