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