使用带有数据对象作为属性的XAML UserControl,作为Gridview - Binding中的DataTemp



当我在"AssetIHM"对象中提供实时数据时,我有一个具有dependencyproperties和Observer模式的对象集合。

 public class assetVM: DependencyObject ,IObserver
{

    public assetVM(AssetIHM aihm) 
    {
        ObserverCollectionSingleton.ObserverCollectionInstance.Add(this);
        _aihm = aihm;
    }
    private string _assetname;
    public string assetname
    {
        get { return _assetname; }
        set
        {
            _assetname = value;
            SetValue(assetnameprop, value);
        }
    }
    public static readonly DependencyProperty assetnameprop =
    DependencyProperty.Register("assetnameprop",    typeof(string),typeof(assetVM), new UIPropertyMetadata(""));
    ...

我也有一个UserControl,它应该显示包含在AssetVM对象中的信息:

 public partial class AssetPanel : UserControl
{ 
    public assetVM Asset 
    {
        get
        {
            return (assetVM)GetValue(assetProperty);
        }
        set
        {
            SetValue(assetProperty, value);
        }
    }
    public static DependencyProperty assetProperty =    DependencyProperty.Register(
       "Asset", typeof(assetVM), typeof(AssetPanel), new   PropertyMetadata(null, new PropertyChangedCallback(OnCurrentItemChanged)));
    public AssetPanel(assetVM _Asset)
    {
        Asset = _Asset;
        this.DataContext = this;
        InitializeComponent();
        ...
    }
    public AssetPanel( )
    {
        this.DataContext = this;
        InitializeComponent();
        ...
    }
    ...

我的主窗口,我有一个列表框,

<Window x:Class="ETRportfolio.MainWindow"        
    DataContext = "{Binding RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:UserControls="clr-namespace:ETRportfolio"  
    Title="MainWindow" Height="1200" Width="1425">

<StackPanel HorizontalAlignment="Left" Height="1153" VerticalAlignment="Top" Width="1400" Margin="10,10,-18,0">
    <ListBox  x:Name="Gridview" Height="800"    >
         <ListBox.ItemTemplate>
            <DataTemplate>
                <UserControls:AssetPanel Asset="{Binding}" />
            </DataTemplate>
         </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

我的问题是,我想为我的Usercontrol提供包含的数据是AssetVM的集合。

public partial class MainWindow : Window
{
    public static Dispatcher curDispatcher;
    public ObservableCollection<assetVM> Datas
    {
        get
        {
            return (ObservableCollection<assetVM>)curDispatcher.Invoke(
                       System.Windows.Threading.DispatcherPriority.DataBind,
                       (DispatcherOperationCallback)delegate { return GetValue(DataSProperty); },
                       DataSProperty);
        }
        set
        {
            curDispatcher.BeginInvoke(DispatcherPriority.DataBind,
                (SendOrPostCallback)delegate { SetValue(DataSProperty, value); },
                value);
        }
    }
    public readonly DependencyProperty DataSProperty = DependencyProperty.Register("DataS", typeof(ObservableCollection<assetVM>), typeof(MainWindow), new PropertyMetadata(null, new PropertyChangedCallback(OnCurrentItemChanged)));
    public MainWindow()
    {
        this.DataContext = this;
        curDispatcher = this.Dispatcher;
        Datas =new ObservableCollection<assetVM>();

        InitializeComponent();
        Gridview.ItemsSource = Datas; 
        addasset.addbtn.Click += onclik;
    }

当创建AssetPanel构造函数时,它不绑定我的AssetVM数据。我总是传递空构造函数。

如何在XAML中做到这一点?
我想问题就在这里:

ListBox.ItemTemplate>
            <DataTemplate>
                <UserControls:AssetPanel Asset="{Binding}" />
            </DataTemplate>
         </ListBox.ItemTemplate>

呢!

编辑::

我删除了this.DataContext = this;

在UserControl构造函数中,但是当一个数据对象

时UserControl构造函数调用
assetVM

被添加到ObservableCollection data中,用作Listview的数据源,这是空构造函数吗?而不是:

 public AssetPanel(assetVM _Asset)
>     {
>         Asset = _Asset; 
>         InitializeComponent();
>         ValuationInfo.ItemsSource = new List<string> { "%", "Value" };
>         AVbox.ItemsSource = Enum.GetValues(typeof(AV)).Cast<AV>();
>     }

所以它不会绑定

Edit2::

 private static void OnCurrentItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AssetPanel instance = (AssetPanel)d;
            instance.Asset = (assetVM)e.NewValue;
            return;
        }

它绑定了!:)

然而,数据对象中的已注册依赖项属性不会显示。

这是我的资产面板xaml:
<UserControl x:Class="ETRportfolio.AssetPanel" 
             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="152" d:DesignWidth="1400">
    <Grid  >
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border BorderBrush="#FFDE6A6A" BorderThickness="1" Grid.Row="0" Grid.Column="0" Background="lightblue">
            <TextBlock x:Name="assetnamebox" TextWrapping="Wrap" Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl }, Path = assetnameprop, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15"/>
        </Border>
   ...

TextBlock x:Name="assetnamebox" texttrapping ="Wrap" Text="{Binding RelativeSource={RelativeSource ancortype =UserControl}, Path = assetnameprop,

没有与assetVM绑定。

怎么了?

呢?

将UserControl的DataContext设置为自身,就像在构造函数中通过语句

完成的那样
this.DataContext = this;

有效地禁用了对继承的DataContexts属性的绑定,如

<ListBox.ItemTemplate>
    <DataTemplate>
        <UserControls:AssetPanel Asset="{Binding}" />
    </DataTemplate>
</ListBox.ItemTemplate>

,其中绑定源是继承的ListBoxItems的DataContext,即assetVM实例。

从UserContr的构造函数中删除DataContext赋值

相关内容

  • 没有找到相关文章

最新更新