Silverlight:将DataGrid的SelectedItem绑定到Customized TabControl

时间:2014-03-18 09:52:39

标签: c# wpf silverlight datagrid tabcontrol

你好我有一个dataGrid如下

<sdk:DataGrid IsReadOnly="True" 
          ItemsSource="{Binding NodeCollection}"
          SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
          SelectionMode="Single"
          AutoGenerateColumns="False">

以前我有另一个DataGrid,它上面填充了来自上面数据网格的SelectedItem的内容。现在我必须引入一个自定义选项卡控件,可以动态添加新选项卡。选项卡项现在必须显示SelectedItem的内容。我有一个按钮(添加新项),它将新标签项(复制上一个标签项)添加到标签控件。

<Button Content="Add New" Command="{Binding AddNewTemplateCommand}"/>

<templateTabs:BindableTabControl MyItemsSource="{Binding ModalityTemplates}">
<templateTabs:BindableTabControl.TabItemTemplate>
                <DataTemplate>
                       <tabView:ModalityTemplateView/>
                </DataTemplate>
<templateTabs:BindableTabControl.TabHeaderItemTemplate>
               <DataTemplate>
                     <StackPanel Orientation="Horizontal" Margin="2,2,2,2">
                     <TextBlock Text="{Binding Header, Mode=TwoWay}" />
                    <Button Height="16" Width="16" Margin="5,0,0,0" Padding="0" Command="{Binding DeleteTemplateCommand}" Content ="X" />
                    </StackPanel>
               </DataTemplate>
</templateTabs:BindableTabControl.TabHeaderItemTemplate>
</templateTabs:BindableTabControl>

目前,我需要从dataGrid中选择项目,然后单击“添加新项”按钮以显示第一个默认选项卡项。但是,我希望只要选择了dataGrid中的项目,就会显示第一个选项卡项。

寻找一个简单的解决方法。

1 个答案:

答案 0 :(得分:0)

由于BindableTabControl是您的代码,因此您可以轻松地向其添加DependencyProperty作为第一个(也是唯一的)项目的可绑定来源:

<sdk:DataGrid x:Name="NodeSelector" ... />
<templateTabs:BindableTabControl
    SingletonItem="{Binding Path=SelectedItem, ElementName=NodeSelector}"
    MyItemsSource="{Binding Path=ModalityTemplates}">
    ...
</templateTabs:BindableTabControl>

您的标签控件需要对此新属性的任何更改做出反应:

public object SingletonItem
{
    get { return (object) GetValue(SingletonItemProperty); }
    set { SetValue(SingletonItemProperty, value); }
}

public static readonly DependencyProperty SingletonItemProperty =
        DependencyProperty.Register("SingletonItem", typeof (object), typeof (BindableTabControl),
        new PropertyMetadata(OnSingletonItemChanged));

public static void OnSingletonItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((BindableTabControl) d).UpdateItems();
}

private void UpdateItems()
{
    //it is up to you to implement this method, so this is only a sketch
    //the null check for SingletonItem is missing too
    if (IsNullOrEmpty(MyItemsSource))
    {
        Items = Enumerable.FromSingleItem(SingletonItem);
    } else {
        Items = MyItemsSource;
    }
}
...
public static void OnMyItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if ( e.OldValue != null ) { RemoveItemsChangedEventHandlers( e.OldValue as INotifyCollectionChanged ); }
    if ( MyItemsSource != null ) { AddItemsChangedEventHandlers( MyItemsSource as INotifyCollectionChanged ); }
    ((BindableTabControl) d).UpdateItems();
}

private void MyItemsSourceItemsChanged(...)
{
    ((BindableTabControl) d).UpdateItems();
}