views:

231

answers:

2

Hi All, 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!

+1  A: 

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"?

wpfwannabe
Outside the Grid, I am using the below code, <TextBlock Text="{Binding Path=OtherSubs}"/> and it works. Could you please show me the code for the above that would work, I am pretty new to WPF and I am not sure how would do this..
developer
My Subscription property is an obsrvablecollection of ProgramSubscriptions object..Please help..
developer
Well, it's a bit confusing. If the above TextBlock works side by side with the DataGrid, then it appears as if TextBlock's and DataGrid's parent DataContext is bound to some ProgramSubscriptions instance and therefore ItemsSource="{Binding Subscription}" cannot work. Which of your classes implement Subscription property?Can you take a look at Debug window? If there are some binding errors, they should be logged there. If you can't decipher them yourself, post them here.
wpfwannabe
I get the below in the Debug window,System.Windows.Data Error: 39 : BindingExpression path error: 'OtherSubs' property not found on 'object' ''ProgramSubscriptionViewModel' (HashCode=1)'. BindingExpression:Path=OtherSubs; DataItem='ProgramSubscriptionViewModel' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
developer
There is your problem. WPF is looking for property OtherSubs on object of type ProgramSubscriptionViewModel and not finding it. Your property is actually defined on ProgramSubscriptions instead. Now it is up to you to put the pieces together.
wpfwannabe
But in that case why is the textblock outside the datagrid able to display properly. If the problem in the datagrid is because I have set its ItemSource to programSubscription, is there not a way I can overwrite it and let OtherSubs get displayed. Could you please show me a workaround for this.I tried to change the dependency property type to ProgramSubscriptionViewModel but it wont work, as it says it is not derived from Dependency object..
developer
Ok, I think I decoded your intentions. Your OtherSubs property is not accessible inside a DataGrid using a simple Binding. You should add a x:Name="_window" to your Window (I presume your Window's DataContext gets changed). You should also do the following modification: <TextBlock Text="{Binding Path=DataContext.OtherSubs,ElementName=_window}"/>
wpfwannabe
But it still displays the data with just <TextBlock Text="{Binding Path=OtherSubs}"/> outside the grid.Is there a way I could create a Datatemplate outside the grid and then include it in the grid..
developer
I do not have any DataContext set in the window..And the above code also doesnt work:(System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=test'. BindingExpression:Path=DataContext.OtherSubs; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
developer
Can you try this instead? <TextBlock Text="{Binding Path=DataContext.OtherSubs,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
wpfwannabe
A: 

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.

Daniel Rose
The Subscription property is a Observable collection of objects..
developer
Your problem probably is a WPF "bug". See http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
Daniel Rose