简体   繁体   中英

WPF datagrid how to get access to the row click

Seems that a WPF application I inherited has a DataGrid

<DataGrid x:Name="dataGrid" ItemsSource="{Binding AllTroubleCalls}" SelectedIndex="{Binding SelectedIndex}" 

I want to end up setting the Background color to yellow only if the Textbox contains text in it.

The text appears when I click on certain rows in the Datagrid

It seems that everything is based upon this "Binding"

  {Binding ... }   

I have a textbox that I added a Name to it

<TextBox ToolTipService.ShowDuration="120000" ToolTip="{Binding ThreatText}" Name="txtThreat" Text="{Binding ThreatText}"  
                 TextWrapping="Wrap" AcceptsReturn="True" 
                 VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"
                 Margin="3" Grid.Row="8"  Grid.Column="1" 
                 Grid.ColumnSpan="3" Grid.RowSpan="1" IsReadOnly="True" Height="30"/>

Show then when I am in a method I can "test" this and it works

  txtThreat.Background = new SolidColorBrush(Colors.Yellow); 

However, I'm not understanding why I cannot get a handle on the data changing from whatever row is clicked on in the Datagrid, that data then appears "magically" in many textboxes etc.. on the xaml page.

I gather that the "Binding" is handling this, but it is also MVVM 2 way binding ?

I have tried plastering so many breakpoints into so many methods but I can't seem to get any of them to show me how the data is changing on row click

There's a little too much going on in this question without the details. However, I can at least answer what I think is your goal. The easiest way to get a textbox to be yellow if there's a text in it is with a style:

    <TextBox >
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Setter Property="Background" Value="Yellow"></Setter>
                <Style.Triggers>
                    <Trigger Property="Text" Value="">
                        <Setter Property="Background" Value="White"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

As for figuring out how the data is changing... The Text displayed in your TextBox is bound to ThreatText. That means the text should change only when a property named ThreatText is changed in a class somewhere (your viewmodel). Have you tried putting a breakpoint on the setter for the ThreatText property? You can also put a breakpoint on the getter as well. It sounds like that when you click in the textbox/row, WPF updates the text in the UI, which means it's reevaluating the binding due to some change in ThreatText; this also means it'll hit the getter... you can check out the stack trace if it does to see what's going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM