Search code examples
c#wpfmvvmdatagridwpfdatagrid

Capture Label text from DataGrid in MVVM WPF


I have implemented a Label in DataGrid using MVVM. My requirement is when user clicks on the Label the events need to be raised which is working fine. But now I would like to capture the label text, but I am unable to acheive this:

<DataGrid HorizontalAlignment="Stretch" Name="DgPreviousEntries" HeadersVisibility="None"
             ItemsSource="{Binding WeeklyWiseEntries}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedweekEntry}">            
            <DataGrid.Columns>
                <DataGridTemplateColumn IsReadOnly="True" Width="600">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Grid>
                                    <Label Grid.Column="0" Grid.Row="1">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                                <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.WeekCommand}">
                                                </i:InvokeCommandAction>
                                            </i:EventTrigger>                                            
                                        </i:Interaction.Triggers>
                                        <Label.Content>
                                            <TextBlock>
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="Mon &#x0a; {0}">
                                                    <Binding Path="Monday" Source="Monday" />                                        
                                                 </MultiBinding>
                                            </TextBlock.Text>
                                            </TextBlock>                                            
                                        </Label.Content> 
                                    </Label>
                                </Grid>
                            </StackPanel>
                        <DataTemplate>                  
                    <DataGridTemplateColumn.CellTemplate>   
                <DataGrid.Columns>
        </DataGrid>

Solution

  • Did you try passing the Label Text as Command Paramter?

    CommandParameter={Binding Path=Content, ElementName=LabelName}

    E.g:

    <Label Grid.Column="0" Grid.Row="1" Name="DayLabel">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction 
            Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.WeekCommand}"
            CommandParameter={Binding Path=Content, ElementName=DayLabel}>
            </i:InvokeCommandAction>
        </i:EventTrigger>                                            
    </i:Interaction.Triggers>
    <Label.Content>
        <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="Mon &#x0a; {0}">
                <Binding Path="Monday" Source="Monday" />                                        
             </MultiBinding>
        </TextBlock.Text>
        </TextBlock>                                            
    </Label.Content> 
    </Label>