【问题标题】:Binding individual ListView items created in XAML绑定在 XAML 中创建的单个 ListView 项
【发布时间】:2013-12-06 10:37:35
【问题描述】:

我有一个 ListViewListViewItem 在 XAML 中静态创建。我想将ListViewItem 的属性绑定到我的视图模型中的不同属性,作为DataContext 给出ListView。但我的代码不起作用:

<ListView AlternationCount="2" Name="listView">        
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Binding.TargetUpdated="TargetUpdated" Text="{Binding Value}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Units}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding About}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>

    <l:PropertyItem Title="title" Value="{Binding Path=DeviceCode}" Units="units" About="about"/>
    <l:PropertyItem Title="title" Value="{Binding Path=VendorName}" Units="units" About="about"/>
    <l:PropertyItem Title="title" Value="{Binding Path=Version}" Units="units" About="about"/>
</ListView>

NumericItem 在哪里:

internal class PropertyItem : DependencyObject
{
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(String), typeof(PropertyItem), new PropertyMetadata(String.Empty));

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(String), typeof(PropertyItem), new PropertyMetadata(String.Empty));

    public static readonly DependencyProperty UnitsProperty =
        DependencyProperty.Register("Units", typeof(String), typeof(PropertyItem), new PropertyMetadata(String.Empty));

    public static readonly DependencyProperty AboutProperty =
        DependencyProperty.Register("About", typeof(String), typeof(PropertyItem), new PropertyMetadata(String.Empty));



    public String Value
    {
        get
        {
            return (String)this.GetValue(ValueProperty);
        }

        set
        {
            this.SetValue(ValueProperty, value);
        }
    }

    public String Title
    {
        get
        {
            return (String)this.GetValue(TitleProperty);
        }

        set
        {
            this.SetValue(TitleProperty, value);
        }
    }

    public String Units
    {
        get
        {
            return (String)this.GetValue(UnitsProperty);
        }

        set
        {
            this.SetValue(UnitsProperty, value);
        }
    }

    public String About
    {
        get
        {
            return (String)this.GetValue(AboutProperty);
        }

        set
        {
            this.SetValue(AboutProperty, value);
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml listview data-binding


    【解决方案1】:

    我找到了解决方案!我的PropertyItem 类派生自DependencyObject,而DependencyObject 又不包含属性DataContext。这就是绑定不起作用的原因。您需要从具有DataContext 属性的人派生,例如FrameworkElement,它会起作用!

    【讨论】:

      【解决方案2】:

      尝试使用这些Bindings:

      <l:PropertyItem Title="title" Value="{Binding Path=DataContext.DeviceCode, 
          RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Units="units" 
          About="about" />
      <l:PropertyItem Title="title" Value="{Binding Path=DataContext.VendorName, 
          RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Units="units"
          About="about"/>
      <l:PropertyItem Title="title" Value="{Binding Path=DataContext.Version, 
          RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Units="units" 
          About="about"/>
      

      在这里,我们绑定到 ListView 类型的父级的 DataContext... 我相信这就是您所追求的。

      【讨论】:

      • 在我从 FrameworkElement 继承 PropertyItem 类之前它不起作用。当我这样做时,它会起作用。但它也适用于更简单的Value={Binding Path=DeviceCode}
      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2014-10-31
      • 2014-12-08
      • 2014-01-10
      • 2016-09-02
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多