Master Detail相同View绑定控件

时间:2010-06-03 07:56:14

标签: wpf data-binding user-controls itemscontrol

说我有ListView ItemControl。以及显示ListView中所选项目的详细信息部分。两者都在同一个xaml页面中。我尝试了一切来完成它,但我错过了什么?

<!-- // List -->
<ItemsControl ItemsSource="{Binding Path=Model, ElementName=SomeListViewControl, Mode=Default}" SnapsToDevicePixels="True" Focusable="False" IsTabStop="False">
    <ItemsControl.ItemTemplate>
            <DataTemplate>
                    <SomeListView:SomeListItemControl x:Name=listItem/>
                </DataTemplate>
        </ItemsControl.ItemTemplate>
</ItemsControl>

<!-- // Details -->
<Label Content="Begindatum" FontSize="16" VerticalAlignment="Center" Grid.Row="1" Margin="2,0,0,0"/>
<TextBox x:Name="Begindatum" Grid.Column="1" Grid.Row="1" Text="{Binding Path=BeginDate, ElementName=listItem,Converter={StaticResource DateTimeConverter}, ConverterParameter=dd-MM-yyyy}" IsEnabled="False" Style="{DynamicResource TextBoxStyle}" MaxLength="30"/>


public event EventHandler<DataEventArgs<SomeEntity>> OnOpenSomething;

public ObservableCollection<SomeEntity> Model
{
            get { return (ObservableCollection<SomeEntity>)GetValue(ModelProperty); }
            set
        {
                Model.CollectionChanged -= new NotifyCollectionChangedEventHandler(Model_CollectionChanged);
                SetValue(ModelProperty, value);
                Model.CollectionChanged += new NotifyCollectionChangedEventHandler(Model_CollectionChanged);
                UpdateVisualState();
    }
}

public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model", typeof(ObservableCollection<SomeEntity>), typeof(SomeListView), new UIPropertyMetadata(new ObservableCollection<SomeEntity>(), new PropertyChangedCallback(ChangedModel)));


private static void ChangedModel(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    SomeListView someListView = source as SomeListView;

    if (someListView.Model == null)
    {
        return;
    }

    CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(someListView.Model);

}

private void Model_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (Model == null)
    {
        return;
    }
}

1 个答案:

答案 0 :(得分:1)

不要使用ItemsControl - ItemsControl没有SelectedItem属性 - 因此您无法确定选择了哪一个。

使用ListBox代替,然后在详细信息部分中创建一个绑定:... DataContext="{Binding SelectedItem,ElementName=ListboxName}" ...其中ListboxNameName属性您使用ListBox