简体   繁体   English

WPF datagrid如何获取对行的单击

[英]WPF datagrid how to get access to the row click

Seems that a WPF application I inherited has a DataGrid 似乎我继承的WPF应用程序具有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 当我单击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. 但是,我不明白为什么我无法从Datagrid中单击的任何行上获取更改后的数据的句柄,然后在xaml页面上的许多文本框等中“神奇地”显示该数据。

I gather that the "Binding" is handling this, but it is also MVVM 2 way binding ? 我收集到“绑定”正在处理此问题,但它也是MVVM 2方式绑定吗?

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. 至于弄清楚数据是如何变化的...在您的文本框中显示的文本已绑定到ThreatText。 That means the text should change only when a property named ThreatText is changed in a class somewhere (your viewmodel). 这意味着仅当在某个地方(您的视图模型)的类中更改了名为ThreatText的属性时,文本才应更改。 Have you tried putting a breakpoint on the setter for the ThreatText property? 您是否尝试过在ThreatText属性的设置器上设置断点? You can also put a breakpoint on the getter as well. 您也可以在getter上设置一个断点。 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; 听起来,当您单击文本框/行时,WPF会更新UI中的文本,这意味着由于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. 这也意味着它将击中吸气剂……您可以检查堆栈跟踪(如果确实如此)以查看发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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