locked
Query Regardings Text binding with Property (/Gridviewcolumn) in WPF RRS feed

  • Question

  • Hi....

            What is the wrong in following ?

     
    <DataTemplate x:Key="ColumnStyle"> 
        < Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" > 
           < Grid Margin = "6,0,6,0" >
               < TextBlock Text = "{Binding Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}} , Path=DisplayMemberBinding}" /> 
           </ Grid >
        </ Border >
     </ DataTemplate > 
    
    

    In My ListView.......I have given th cell reference .....as follows.....

    < GridViewColumn Header = "Project" Width = "Auto" x:Name="grdvchProject" DisplayMemberBinding = "{Binding Path=Project}" CellTemplate = "{StaticResource ColumnStyle}" > </ GridViewColumn >
    
    < GridViewColumn Header = "Task" Width = "Auto" x:Name="grdvchTask" DisplayMemberBinding = "{Binding Path=Task}" CellTemplate = "{StaticResource ColumnStyle}" > </ GridViewColumn > 
    
    < GridViewColumn Header = "Work" Width = "Auto" x:Name="grdvchWork" DisplayMemberBinding = "{Binding Path=Work}" CellTemplate = "{StaticResource ColumnStyle}" > </ GridViewColumn > 
    
    < GridViewColumn Header = "Hours" Width = "Auto" x:Name="grdvchHours" DisplayMemberBinding = "{Binding Path=Hours}" CellTemplate = "{StaticResource ColumnStyle}" > </ GridViewColumn > 
    
    < GridViewColumn Header = "Idle" Width = "Auto" x:Name="grdvchIdle" DisplayMemberBinding = "{Binding Path=Idle}" CellTemplate = "{StaticResource ColumnStyle}" > </ GridViewColumn > 
    
    
    




    In My Application there are 30 columns ..SO I dont want to right cell template at each & every column individually ..thats why I defined data template & give reference.....

    But above code doesnt work Properly It dose not Bind the Property to my datatemplate's textblock....
    SO please tell me what is the wrong in above ....

    Thanks in advance....
    Tuesday, October 20, 2009 12:23 PM

Answers

  • Hi Hardik,

    It's no need to specify a binding for the DisplayMemberBinding property if you want to use you custom cell template. And in your custom data template, just specify the path that you want to binding, it's no need to find the GridViewColumn object for binding. The following should work:

        <Window.Resources>
            <DataTemplate x:Key="columnstyle1">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock Text="{Binding Path=Project}" />
                    </Grid >
                </Border >
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListView Name="listView1" Margin="12,29,12,59" ItemsSource="{Binding}">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn CellTemplate="{StaticResource columnstyle1}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>


    If you have any questions or concerns,please feel free to let me know.

    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Hardik wants to avoid write a cell template for each column, but the problem is how to set the the binding for 30 different columns. With this solution you solve the problem for only one column (Project column).

    So, I repeat: "I think that you will need to write a data template per column."

    Thanks.
    Tuesday, October 27, 2009 8:05 PM
  • Hello,

    Try this:

    <DataTemplate x:Key="testTemplate">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock x:Name="txt" Text="{Binding Path=Project}"   TextTrimming="CharacterEllipsis"
                                                                  ToolTip ="{Binding OS}"/>
                    </Grid >
                </Border >
          <DataTemplate.Triggers>
                  <DataTrigger Binding="{Binding OS}" Value="">
                       <Setter TargetName="txt" Property="ToolTip" Value="{x:Null}" />
                  </DataTrigger>
         </DataTemplate.Triggers>
    </DataTemplate>

    Good Luck!
    Wednesday, October 28, 2009 1:22 PM

All replies

  • Hello,

    The column ignores the data template because you are setting the property DisplayMemberBinding...
    Tuesday, October 20, 2009 2:03 PM
  • I think that you will need to write a data template per column.

    Good Luck.
    • Proposed as answer by Olaf Rabbachin Monday, October 26, 2009 10:30 AM
    Saturday, October 24, 2009 2:42 PM
  • Hi Hardik,

    I'm not sure as to whether I really get exactly what your after, but - FWIW - there'll just have to be a place where you define which column binds to what field, so (in your XAML) there's absolutely no sense in defining the CellTemplate in order to setup the binding. Hence, just skip the CellTemplate and setup your 30 columns manually.  If, OTOH, your goal is to define a style (i.e. the visual appearance) for your columns, then don't go for a <DataTemplate> but rather define a <Style>.

    If that doesn't help at all, try to be more specific in what you're really after as this is not all too clear.

    Cheers,
    Olaf
    Saturday, October 24, 2009 3:25 PM
  • Hi...

             Thanks for reply....

             Exactly my aim is defining a style(i.e. the visual appearance).  So can you please suggest me How can I define the cell template in my style....If possible for you the explain me with example......


             Thanks A Lot.... 
    Sunday, October 25, 2009 1:21 PM
  • Hi Hardik,

             Exactly my aim is defining a style(i.e. the visual appearance).  So can you please suggest me How can I define the cell template in my style....If possible for you the explain me with example......

    I suggest you read through this CodeProject article (FWIW - the other parts of the series are truly worth reading, too) for the basics.

    When you then want to customize your controls' appearance, one of your first stops should be this MSDN section - it contains samples of ControlTemplates for the most common controls.

    Regarding the ListView control specifically, this blog contains another bunch of things the other links do not provide.

    Cheers,
    Olaf
    Sunday, October 25, 2009 2:59 PM
  • Hello Olaf...


              Thanks for reply....

              Still I dont get the solution...

              I have already refered as you suggested, Also the example given in the blog which you have suggest ,In that example Also cell template define individually for each GridViewColumn ....My Question is ,Am I able to define cell template in my Style ..If Yes Then How?....Please guide me.....

              Thanks....... 
             
    Monday, October 26, 2009 6:20 AM
  • Hi Hardik,

    sorry for misguiding you - I personally haven't bothered with styling GridViewColumns as of yet and so simply assumed it was possible (so much for assumptions). However, I just found this thread here on the forum which indicates that this just isn't possible. Now, since using a DataTemplate and DisplayMemberBinding at the same time generally is a bad idea (it won't work), we're back to what HomeroThompson suggested - you'll probably have to define a DataTemplate for each column and drop the DisplayMemberBinding in the columns themselves.

    Cheers,
    Olaf
    Monday, October 26, 2009 10:11 AM
  • HI...

          Thanks for reply...

          you suggest a nice thread for this query...

          Thanks
    Monday, October 26, 2009 10:29 AM
  • Hi Hardik,

    It's no need to specify a binding for the DisplayMemberBinding property if you want to use you custom cell template. And in your custom data template, just specify the path that you want to binding, it's no need to find the GridViewColumn object for binding. The following should work:

        <Window.Resources>
            <DataTemplate x:Key="columnstyle1">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock Text="{Binding Path=Project}" />
                    </Grid >
                </Border >
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListView Name="listView1" Margin="12,29,12,59" ItemsSource="{Binding}">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn CellTemplate="{StaticResource columnstyle1}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>


    If you have any questions or concerns,please feel free to let me know.

    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Zhi-Xin Ye Tuesday, October 27, 2009 12:42 PM
    • Unmarked as answer by Bansari Thakker Wednesday, October 28, 2009 7:09 AM
    Monday, October 26, 2009 12:09 PM
  • Hi Hardik,

    It's no need to specify a binding for the DisplayMemberBinding property if you want to use you custom cell template. And in your custom data template, just specify the path that you want to binding, it's no need to find the GridViewColumn object for binding. The following should work:

        <Window.Resources>
            <DataTemplate x:Key="columnstyle1">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock Text="{Binding Path=Project}" />
                    </Grid >
                </Border >
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListView Name="listView1" Margin="12,29,12,59" ItemsSource="{Binding}">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn CellTemplate="{StaticResource columnstyle1}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>


    If you have any questions or concerns,please feel free to let me know.

    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Hardik wants to avoid write a cell template for each column, but the problem is how to set the the binding for 30 different columns. With this solution you solve the problem for only one column (Project column).

    So, I repeat: "I think that you will need to write a data template per column."

    Thanks.
    Tuesday, October 27, 2009 8:05 PM
  • HI... HOmero..

     Thanks for your reply

        I have another query regarding this....In this datatemplate  have added Tooltip to show each cell's tooltip. Now the problem is When Textblock's text is Blank OR path property is blank string Still It shows Tooptip as blank tooltip Which looks quite not good ...I have tried to set triggers in data template for do not show blank tooltip...But It dosnt work...So can you suggest me....How can I get this?


    <Window.Resources>
            <DataTemplate x:Key="columnstyle1">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock Text="{Binding Path=Project}"   TextTrimming="CharacterEllipsis"
                                                                   ToolTipService.ToolTip ="{Binding OS}"/>

                    </Grid >
                </Border >
            </DataTemplate>
        </Window.Resources>

    Thanks...
    Wednesday, October 28, 2009 6:10 AM
  • Hi Hardik,

    if HomeroThompsons posting answered your question, then please unmark Zhi-Xin's answer and mark his reply as your answer (that helps others find the right one).

    Regarding your empty Tooltip, you'll have to use some code for that. Here's a link that should get you going.

    Cheers,
    Olaf
    Wednesday, October 28, 2009 6:51 AM
  • Hello,

    Try this:

    <DataTemplate x:Key="testTemplate">
                <Border BorderBrush = "Black" BorderThickness = "2,0,2,0" Margin = "-6,0,-6,0" >
                    <Grid Margin = "6,0,6,0" >
                        <TextBlock x:Name="txt" Text="{Binding Path=Project}"   TextTrimming="CharacterEllipsis"
                                                                  ToolTip ="{Binding OS}"/>
                    </Grid >
                </Border >
          <DataTemplate.Triggers>
                  <DataTrigger Binding="{Binding OS}" Value="">
                       <Setter TargetName="txt" Property="ToolTip" Value="{x:Null}" />
                  </DataTrigger>
         </DataTemplate.Triggers>
    </DataTemplate>

    Good Luck!
    Wednesday, October 28, 2009 1:22 PM
  • Hi  Homero...

                    Thanks Dear...

                    This works perfectly...

                     Thanks A Lot....
    Wednesday, October 28, 2009 1:45 PM