Home Forums WPF controls Xceed DataGrid for WPF Buttons in rows with parameters

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22786 |

    Basically what I want to do is have a button in the first column of each row. Somehow I need to tie the id (or whatever key field) into the button so that I know which row the button was clicked from when I get into the function in the code-behind.

    For example, I want a button for ‘Add’ in the first column of every row. When the user clicks one of the Add buttons, I want to be able to determine: which row was clicked or pick up the unique id from that row so that I can add this record to another datasource essentially.

    Here is what I tried to do, but it is not working.

    1. Added to top of my page in page declaration
    xmlns:local=”clr-namespace:MyAppNamespace”

    2. Added these commandbindings for the page
    <Page.CommandBindings>
    <CommandBinding Command=”local:GridCommands.AddProgram”
    Executed=”OnAddProgram” />
    </Page.CommandBindings>

    3. My DataGrid markup looks like this (mainly the DataTemplate is the key part:
    <xcdg:DataGridControl x:Name=”gridProgramsAll” Margin=”5″ View=”TableView.Aero.NormalColor”
    ReadOnly=”True” SelectionMode=”Single” ClipToBounds=”True”
    ContextMenuService.HasDropShadow=”False” Background=”#FFFFFFFF”
    BorderBrush=”#FFFFFFFF” HorizontalAlignment=”Stretch”
    ItemsPrimaryAxis=”Vertical”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column Width=”30″ FieldName=”MyField”>
    <xcdg:Column.CellContentTemplate>
    <DataTemplate>
    <Button Content=”+” Foreground=”White” Command=”local:GridCommands.AddProgram”>
    <Button.CommandParameter>
    <Binding Path=”Id”/>
    </Button.CommandParameter>
    </Button>
    </DataTemplate>
    </xcdg:Column.CellContentTemplate>
    </xcdg:Column>
    <xcdg:Column FieldName=”ProjectId” Title=”Project ID”/>
    <xcdg:Column FieldName=”Name” Title=”Program Name”/>
    <xcdg:Column FieldName=”Desc” MinWidth=”200″ Title=”Program Description”/>
    <xcdg:Column FieldName=”Title” Title=”Program Title”/>
    <xcdg:Column FieldName=”ImageId” Title=”Image ID”/>
    <xcdg:Column FieldName=”Help” Title=”Program Help”/>
    <xcdg:Column FieldName=”Active” Title=”Active”/>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

    4. I also have this inside a GridCommands class:
    public static RoutedCommand AddProgram =
    new RoutedCommand(“AddProgram”, typeof(Pages.Programs));

    5. Lastly I have this in my code-behind:
    void OnAddProgram(object sender, ExecutedRoutedEventArgs args) {
    string parameter = args.Parameter.ToString();
    }

    It works find and gets into the function in the code behind but the Parameter is always null. If I change the DataTemplate and remove the binding part and just add in like “FakeParam” for CommandParameter, then the function finds the parameter. Basically I don’t think my binding is set up right.

    Maybe there is a way easier way to do this, im not sure.

    Thanks for the help,

    Doug

    Imported from legacy forums. Posted by Douglas (had 4586 views)

    Xceed Support
    Member
    Post count: 5658

    When you are specifying the first Column, you are specifying the “MyField” field name… What this does is that it indicates to the DataGridControl that each cell’s content (for the “MyField” column) should map to the “MyField” property of the Data Item ( and I’m supposing, based on the other column’s names that the “MyField” property does not exist on the data item).

    <b>1. </b>You can overrule the “default” binding for the column by specifying your own:

    <xcdg:Column Width=”30″ FieldName=”MyField” <b>DisplayMemberBinding=”{Binding .}”</b>>

    In the above case, binding the column to . causes the cells to contain the whole data item. Binding the CellContentTemplate’s CommandParameter to “Id” would then propagate the “Id” property of the data item.

    <b>2. </b>Alternatly, you could bind the “Id” property directly to the “MyField” column:

    <xcdg:Column Width=”30″ FieldName=”MyField” <b>DisplayMemberBinding=”{Binding Id}”</b>>

    But then, the CommandParameter binding would be <Binding Path=”.”/>

    <b>3. </b>Finally, you can send as CommandParameter the whole data item ( in case its easier for you to handle a data item in your command handler):

    <xcdg:Column Width=”30″ FieldName=”MyField” <b>DisplayMemberBinding=”{Binding .}”</b>>

    And then, in the CommandParameter binding: <Binding Path=”.”/>

    Let me know if this addresses your needs.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 306 views)

    User (Old forums)
    Member
    Post count: 23064

    You could have ONE add button at the top bottom or whereever of the window/form/control you are creating, Then when that button is clicked you get the CurrentItem in the DataGridControl (which is really a reference to your actual datasource) and add that to another datasoure

    seems simpler to me

    Imported from legacy forums. Posted by MiddleTommy (had 544 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks alot Marc. I got it working now. I completely missed that FieldName part. I had copied that from another post. It really shouldnt have been “MyField” it should have been “Id” and then i used <Binding Path=”.”/>

    Thanks again,

    Doug

    Imported from legacy forums. Posted by Douglas (had 392 views)

    User (Old forums)
    Member
    Post count: 23064

    I tried a similar approach but my button is always disabled in DataGrid 1.2. Any idea?
    <pre>
    <xcdg:Column FieldName=”Delete” Title=”” Width=”18″ DisplayMemberBinding=”{Binding Path=., Mode=TwoWay}” CellEditorDisplayConditions=”CellIsCurrent” >
    <xcdg:Column.CellContentTemplate>
    <DataTemplate>
    <Button xcdg:DataCell.IsCellFocusScope=”True” Command=”pres:GlobalCommands.MyCommand” CommandParameter=”{Binding RelativeSource={RelativeSource TemplatedParent}}” >
    <Image Source=”{StaticResource deleteImage}” />
    </Button>
    </DataTemplate>
    </xcdg:Column.CellContentTemplate>
    </xcdg:Column>
    </pre>

    Imported from legacy forums. Posted by nat (had 374 views)

    User (Old forums)
    Member
    Post count: 23064

    BTW, I already have command binding as follows:
    <pre>
    <CommandBinding Command=”pres:GlobalCommands.MyCommand” Executed=”MyCommandExecuted” CanExecute=”MyCommandCanExecute” />
    </pre>
    in which MyCommandCanExecute always set e.CanExecute to true

    Imported from legacy forums. Posted by nat (had 362 views)

    User (Old forums)
    Member
    Post count: 23064

    Can someone help me on this? I’m pretty stuck now 🙁

    Imported from legacy forums. Posted by nat (had 5654 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.