0
votes

I want to Bind the textblock text in WPF datagrid to a dependency property. Somehow, nothing gets displayed, but when I use the same textblock binding outside the grid, everything works fine. Below is my code,

        </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>

Code-Behind

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 + ", ";
            }
        }

Please do let me know if there is another way that i can make this work, instead of using a dependencyproperty, althouht for testing I did put a textblock below datagrid, and it works perfectly fine.. Help!

2
I assume that OtherSubs is part of a class that is in a collection, and that the ItemsSource of the DataGrid is set to this collection? - Wonko the Sane

2 Answers

2
votes

Your Subscription property must be a collection of ProgramSubscriptions objects. It must support at least IEnumerable interface. Normally, you would have something like List<ProgramSubscriptions>. Additionally, OtherSubs is obviously a property on ProgramSubscriptions and this is ok.

Can you please show how you use "the same textblock binding outside the grid"?

0
votes

You are binding the DataGrid to Subscription. This would have to be a property on whatever the DataGrid's DataContext is. As wpfwannabe said, it should support IEnumerable. Ideally, you would have an ObservableCollection<> or derived, so the DataGrid updates automatically.

From there the DataGrid will get the items it should display. To display actual data, you have your DataGridTemplateColumn definition. Since you bind to OtherSubs, this means that the objects enumerated by your Subscription IEnumerable should have that property. BTW it doesn't need to be a Dependency Property for this to work.