ObservableCollection始终只为每个创建的TabItem

时间:2015-08-16 19:37:50

标签: c# wpf binding observablecollection datacontext

有人可以指出我的错误请求。 当我添加选项卡时,每个选项卡都会复制在第一个创建的选项卡中添加的文本框中的数据。 ObservableCollection仅为每个Tab获取第一项。

我有使用TabControl的MainView,我以编程方式添加Tabs并通过button_click为其设置内容

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:y="clr-namespace:Test"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ToolBar Height="40" VerticalAlignment="Top">
        <Button Foreground="AliceBlue" FontWeight="Normal" Click="Button_Click" FontSize="14" FontFamily="Fixed Miriam Transparent">Menu
            <Button.ContextMenu>
                <ContextMenu >
                    <MenuItem Header="Add Invoice" Click="AddInvoice_Click"/>
                    <MenuItem Header="Invoices List" Click="InvoicesList_Click"/>
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
        <ToolBar.Background>
            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                <GradientStop Color="Black" Offset="0" />
                <GradientStop Color="#FF173ADE" Offset="0.431" />
                <GradientStop Color="#FF0B1D6F" Offset="0.646" />
            </LinearGradientBrush>
        </ToolBar.Background>
    </ToolBar>
    <DockPanel Margin="2,46,0,0" Name="dockPanel1" Width="Auto">
    <Grid>
    <TabControl Name="tabCon"
        >
            </TabControl>
        </Grid >
            </DockPanel>
        </Grid>

通过button_click我添加了Tabs,为它们设置了内容,为控件设置了Datacontext

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    MVM mvm = new MVM();
    MVM2 mvm2 = new MVM2();

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        (sender as Button).ContextMenu.IsEnabled = true;
        (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
        (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
        (sender as Button).ContextMenu.IsOpen = true;
    }


    private void AddInvoice_Click(object sender, RoutedEventArgs e)
    {
        mvm2.Invoice_Items.Add(new VM2());
        mvm.Itemz.Add(new VM());
        var tabItem = new TabItem();
        TabView tv = new TabView();
        tabItem.Content = tv;
        string s = string.Format("Tab");
        mvm.Itemz.Add(new VM(s));
        xmx.Itemz.Add(new VM());
        tv.dataGrid1.DataContext = mvm2;
        tv.listBox1.DataContext = mvm;
            tabCon.Items.Add(tabItem);
            if (tabCon.SelectedIndex == null)
            {
                tabCon.SelectedIndex = -1;
            }
            tabCon.SelectedIndex++;

    }

}

}

这是标签的视图

<UserControl x:Class="Test.TabView"
         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" 
         xmlns:views="clr-namespace:Test"
         mc:Ignorable="d" 
         d:DesignHeight="600" d:DesignWidth="800">
<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="*" MinHeight="300" />
        <RowDefinition Height="*" MinHeight="300" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />

    </Grid.ColumnDefinitions>
    <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 Path = Invoice_Items}"
              IsReadOnly="False" SelectionUnit="Cell" CanUserAddRows="True" IsSynchronizedWithCurrentItem="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Item Name" Binding="{Binding Item_Name, Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>
    <ListBox ItemsSource="{Binding Itemz}" SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch" Name="listBox1" VerticalAlignment="Top" IsEnabled="True" Focusable="True" SelectionMode="Single" VerticalContentAlignment="Stretch" MinWidth="768" MinHeight="446" Opacity="1" HorizontalContentAlignment="Stretch">
        <ListBox.BorderBrush>
            <SolidColorBrush />
        </ListBox.BorderBrush>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <views:DataGrrr></views:DataGrrr>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.Background>
            <SolidColorBrush />
        </ListBox.Background>
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border x:Name="Bd"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive"
                                        Value="False" />
                                        <Condition Property="IsSelected"
                                        Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background"
                                TargetName="Bd"
                                Value="Transparent" />
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive"
                                        Value="True" />
                                        <Condition Property="IsSelected"
                                        Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background"
                                TargetName="Bd"
                                Value="Transparent" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.Resources>
    </ListBox>


</Grid>

public partial class TabView : UserControl
{
    MVM xmx = new MVM();
    MVM2 mvm = new MVM2();
    public TabView()
    {

        dataGrid1 = new DataGrid();
        InitializeComponent();
        //mvm.Invoice_Items.Add(new VM2());
        //xmx.Itemz.Add(new VM());
        //listBox1.DataContext = xmx;
        //dataGrid1.DataContext = mvm;

    }

}

这是ListBox

的观点
<UserControl x:Class="Test.DataGrrr"
         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="456" d:DesignWidth="887">
<Grid Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="768" MinHeight="446" Focusable="True" Height="Auto">
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="43" Height="*" />
        <RowDefinition Height="*" MinHeight="45" />
        <RowDefinition Height="*" MinHeight="45" />
        <RowDefinition Height="*" MinHeight="170" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" MinWidth="100" />
        <ColumnDefinition Width="*"  />
        <ColumnDefinition Width="*"  />
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding Supplier, Mode=TwoWay}" 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" />
</Grid>

ObservableCollection之一

class MVM : BVM
{
    private ObservableCollection<VM> items = new ObservableCollection<VM>();

    public ObservableCollection<VM> Itemz
    {
        get
        {
            return items;
        }
        set
        {
            items = value;
            OnPropertyChanged("Itemz");
        }
    }

}

和模型

    class VM: BVM
{
    private string supplier;


    public VM()
    {

    }

    public string Supplier
    {
        get
        {
            return supplier;
        }

        set
        {
            if (supplier != value)
            {
                supplier = value;
                OnPropertyChanged("Supplier");
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

使用列表并检查结果,但我认为问题不在ObservableCollection中, 问题是在你绑定到tab的类中,它就像你将一个对象绑定到每个新选项卡一样,就像静态