Dependency Property WPF Grid

Posted by developer on Stack Overflow See other posts from Stack Overflow or by developer
Published on 2010-04-22T15:37:58Z Indexed on 2010/04/22 19:13 UTC
Read the original article Hit count: 924

Filed under:
|
|

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,

<Window.Resources>
        <Style x:Key="cellCenterAlign" TargetType="{x:Type toolkit:DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type toolkit:DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type toolkit:DataGridColumnHeader}">
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="RoleValues">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="domain:SubscriptionRole"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <DataTemplate x:Key="myTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Path=OtherSubs}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="220"/>
            <RowDefinition Height="Auto"/>
        </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 programSubscription}"
                                              ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" 
                                              SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="450"
                              ScrollViewer.VerticalScrollBarVisibility="Auto" Height="200">
                <toolkit:DataGrid.Columns>
                    <toolkit:DataGridTextColumn Header="Program" Width="80" Binding="{Binding Program.JobNum}" CellStyle="{StaticResource cellCenterAlign}" IsReadOnly="True"/>

                    <toolkit:DataGridTemplateColumn Header="Role" Width="80" CellStyle="{StaticResource cellCenterAlign}">
                        <toolkit:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox SelectedItem="{Binding Role}" ItemsSource="{Binding Source={StaticResource RoleValues}}" Width="70">
                                <ComboBox.Style>
                                    <Style>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Role}" Value="Owner">
                                                <Setter Property="ComboBox.Focusable" Value="False"/>
                                                <Setter Property="ComboBox.IsEnabled" Value="False"/>
                                                <Setter Property="ComboBox.IsHitTestVisible" Value="False"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ComboBox.Style>
                                </ComboBox>
                            </DataTemplate>
                        </toolkit:DataGridTemplateColumn.CellTemplate>
                    </toolkit:DataGridTemplateColumn>

                    <toolkit:DataGridCheckBoxColumn Header="Email" Width="60" Binding="{Binding ReceivesEmail}" CellStyle="{StaticResource cellCenterAlign}"/>
                    <!--<toolkit:DataGridTextColumn Header="Others" Width="220" Binding="{Binding programSubscription1.Subscriber.Username}" CellStyle="{StaticResource cellCenterAlign}" IsReadOnly="True"/>-->

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


        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
            <CheckBox Content="Show Only Active Programs" IsChecked="True" Margin="0,0,8,0"/>
        </StackPanel>

        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Grid.Column="1" HorizontalAlignment="Right">
            <Button Content="Save" Height="23" Width="75" Margin="0,0,8,0" Click="Save_Click"/>
            <Button Content="Cancel" Height="23" Width="75" Margin="0,0,8,0" Click="Cancel_Click" /> 
        </StackPanel>
     </Grid>
    </Grid>

Code-Behind

public partial class ProgramSubscriptions : Window { public static ObservableCollection programSubscription { get; set; }

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));


private string CurrentUsername = "test";

public ProgramSubscriptions()
{
    InitializeComponent();
    DataContext = this;
    LoadData();
}


protected void LoadData()
{
    programSubscription = new ObservableCollection<ProgramSubscriptionViewModel>();


    if (res != null && res.TotalResults > 0)
    {
        List<ProgramSubscriptionViewModel> UserPrgList = new List<ProgramSubscriptionViewModel>();

        //other....
        List<ProgramSubscriptionViewModel> OtherPrgList = new List<ProgramSubscriptionViewModel>();
        ArrayList myList = new ArrayList();

        foreach (DomainObject obj in res.ResultSet)
        {
            ProgramSubscription prg = (ProgramSubscription)obj;
            if (prg.Subscriber.Username == CurrentUsername)
            {
                UserPrgList.Add(new ProgramSubscriptionViewModel(prg));
                myList.Add(prg.Program.ID);
            }
            else
                OtherPrgList.Add(new ProgramSubscriptionViewModel(prg));
        }

        for (int i = 0; i < UserPrgList.Count; i++)
        {
            ProgramSubscriptionViewModel item = UserPrgList[i];
            programSubscription.Add(item);
        }

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

}

I posted the entire code. What exactly I want to do is in the datagridtemplatecolumn for others I want to display the usernames that are not in CurrentUsername, but they have the same program Id as the CurrentUsername. 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!

© Stack Overflow or respective owner

Related posts about wpfdatagrid

Related posts about binding