【问题标题】:Dependency Property WPF Grid依赖属性 WPF 网格
【发布时间】:2010-04-22 15:37:58
【问题描述】:

我想将 WPF 数据网格中的文本块文本绑定到依赖属性。不知何故,什么都没有显示,但是当我在网格外使用相同的文本块绑定时,一切正常。下面是我的代码,

        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <toolkit:DataGrid Name="definitionGrid" Margin="0,10,0,0" AutoGenerateColumns="False" 
                                              CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False"
                                              RowHeight="25" FontWeight="Normal" ItemsSource="{Binding Subscription}"
                                              ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" 
                                              SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="450"
                              ScrollViewer.VerticalScrollBarVisibility="Auto" Height="200">          
                    <toolkit:DataGridCheckBoxColumn Header="Email" Width="60" Binding="{Binding ReceivesEmail}" CellStyle="{StaticResource cellCenterAlign}"/>

                    <toolkit:DataGridTemplateColumn Header="Others" Width="220" CellStyle="{StaticResource cellCenterAlign}" IsReadOnly="True">
                        <toolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=OtherSubs}"/>
                            </DataTemplate>
                        </toolkit:DataGridTemplateColumn.CellTemplate>
                    </toolkit:DataGridTemplateColumn>
                </toolkit:DataGrid.Columns>
            </toolkit:DataGrid>   
            <TextBlock Text="{Binding Path=OtherSubs}"/>
       </StackPanel>

代码隐藏

public string OtherSubs
{
    get { return (string)GetValue(OtherSubsProperty); }
    set { SetValue(OtherSubsProperty, value); }
}
public static readonly DependencyProperty OtherSubsProperty = DependencyProperty.Register("OtherSubs", typeof(string), 
    typeof(ProgramSubscriptions), new UIPropertyMetadata(string.Empty));

        //other....
        for (int i = 0; i < OtherPrgList.Count; i++)
        {
            foreach (int y in myList)
            {
                ProgramSubscriptionViewModel otheritem = OtherPrgList[i];
                if (y == otheritem.Program.ID)
                    OtherSubs += otheritem.Subscriber.Username + ", ";
            }
        }

请让我知道是否有另一种方法可以使这项工作,而不是使用依赖属性,尽管为了测试,我确实在数据网格下方放置了一个文本块,它工作得非常好.. 帮助!

【问题讨论】:

  • 我假设 OtherSubs 是集合中的类的一部分,并且 DataGrid 的 ItemsSource 设置为此集合?

标签: wpf binding wpfdatagrid


【解决方案1】:

您的 Subscription 属性必须是 ProgramSubscriptions 对象的集合。它必须至少支持 IEnumerable 接口。通常,您会有类似 List 的内容。此外,OtherSubs 显然是 ProgramSubscriptions 上的一个属性,这没关系。

您能否说明您如何使用“网格外的相同文本块绑定”?

【讨论】:

  • 在网格之外,我正在使用下面的代码, 并且它可以工作。你能告诉我上面的代码吗?我对 WPF 很陌生,我不知道该怎么做..
  • 我的订阅属性是 ProgramSubscriptions 对象的 obsrvablecollection..请帮助..
  • 嗯,这有点令人困惑。如果上述 TextBlock 与 DataGrid 并排工作,则 TextBlock 和 DataGrid 的父 DataContext 似乎绑定到某个 ProgramSubscriptions 实例,因此 ItemsSource="{Binding Subscription}" 无法工作。您的哪些类实现了 Subscription 属性?你能看看调试窗口吗?如果有一些绑定错误,它们应该被记录在那里。如果您自己无法破译,请在此处发布。
  • 我在调试窗口中得到以下信息,System.Windows.Data 错误:39:BindingExpression 路径错误:在 'object' ''ProgramSubscriptionViewModel' (HashCode=1)' 上找不到 'OtherSubs' 属性. BindingExpression:Path=OtherSubs; DataItem='ProgramSubscriptionViewModel' (HashCode=1);目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)
  • 你可以试试这个吗?
【解决方案2】:

您正在将 DataGrid 绑定到订阅。无论 DataGrid 的 DataContext 是什么,这都必须是一个属性。正如 wpfwannabe 所说,它应该支持 IEnumerable。理想情况下,您应该有一个 ObservableCollection&lt;&gt; 或派生的,因此 DataGrid 会自动更新。

DataGrid 将从那里获取它应该显示的项目。要显示实际数据,您需要定义 DataGridTemplateColumn。由于您绑定到 OtherSubs,这意味着您的订阅 IEnumerable 枚举的对象应该具有该属性。顺便说一句,它不需要是依赖属性才能工作。

【讨论】:

猜你喜欢
  • 2014-05-23
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 2013-01-31
  • 2011-03-28
相关资源
最近更新 更多