0

I am trying to create a DataGrid that has "clickable" text located within it. In the example I provide I would like to click on the Bid or Ask price and have a handler triggered. I guess it would be like having a hyperlink that triggers a function instead of a browser. The handler will be used to populate a list with the data located in the selected Bid or Ask row. I'm not sure how to approach this and any advice will be greatly appreciated.

What I have now:

XAML

 <UserControl.Resources>
     <local:Options x:Key="Options"/>
 </UserControl.Resources>

 <DataGrid AutoGenerateColumns="True"
              Grid.Row="0"
              Grid.Column="1"
              RowHeaderWidth="0"
              AlternationCount="2"
              x:Name="AGrid"
              ItemsSource="{StaticResource Options}"
              CanUserAddRows="False"
              ColumnWidth="*"
              IsReadOnly="True">
 </DataGrid>

C#

public class Option
{
    public int Strike { get; set; }
    public int Bid { get; set; }
    public int Ask { get; set; }

    public Option()
    {
    }
}

public class Options : List<Option>
{
    public Options()
    {

        this.Add(new Option() { Strike = 120, Bid = 20, Ask = 25 });
        this.Add(new Option() { Strike = 130, Bid = 30, Ask = 35 });
        this.Add(new Option() { Strike = 140, Bid = 40, Ask = 45 });           
    }
}  
}
Wallace
  • 572
  • 3
  • 15
  • I think you are looking for a hyperlink button http://stackoverflow.com/questions/5122334/how-do-i-make-a-wpf-button-look-like-a-link – pushpraj Jun 05 '14 at 02:46

1 Answers1

0

You should use columnheaderstyle and set template as button if you want button for only column headers otherwise you can use celltemplate.

Following code should work as per your requirement.I have implemented it only for column headers.

 <DataGrid ItemsSource="{Binding DataGridData}">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Button Content="{Binding}">
                                    <Button.Style>
                                        <Style      TargetType="Button">
                                            <Setter  Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate  TargetType="Button">
                                                        <TextBlock  TextDecorations="Underline" FontWeight="Bold">
                        <ContentPresenter  />
                                                        </TextBlock>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>
DT sawant
  • 1,201
  • 2
  • 10
  • 21