C# 在代码隐藏中的 DataTemplate 中查找 WPF 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11826272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:24:02  来源:igfitidea点击:

Find a WPF element inside DataTemplate in the code-behind

c#wpfxamldatatemplatebar-chart

提问by Robert Langdon

I have a data-template

我有一个数据模板

<Window.Resources>
         <DataTemplate x:Key="BarChartItemsTemplate">
         <Border Width="385" Height="50">
            <Grid>
               <Rectangle Name="rectangleBarChart" Fill="MediumOrchid" StrokeThickness="2" Height="40" Width="{Binding}" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                  <Rectangle.LayoutTransform>
                     <ScaleTransform ScaleX="4"/>
                  </Rectangle.LayoutTransform>
               </Rectangle>
               <TextBlock Margin="14" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding}">
                  <TextBlock.LayoutTransform>
                     <TransformGroup>
                        <RotateTransform Angle="90"/>
                        <ScaleTransform ScaleX="-1" ScaleY="1"/>
                     </TransformGroup>
                  </TextBlock.LayoutTransform>
               </TextBlock>
            </Grid>
         </Border>
      </DataTemplate>
  </Window.Resources>

I have a button on the form. I need to change the scale(scaleTransform) the rectangle from the dataTemplate. How am I supposed to access the 'rectangleBarChart' element in the Button_Click event of the above mentioned button ?

我在表格上有一个按钮。我需要从 dataTemplate 更改矩形的比例(scaleTransform)。我应该如何访问上述按钮的 Button_Click 事件中的 'rectangleBarChart' 元素?

采纳答案by Mark Oreta

I use this function a lot in my WPF programs to find children elements:

我在 WPF 程序中经常使用这个函数来查找子元素:

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
   if (depObj != null)
   {
       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
       {
           DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

           if (child != null && child is T)
               yield return (T)child;

           foreach (T childOfChild in FindVisualChildren<T>(child))
               yield return childOfChild;
       }
   }
}

Usage:

用法:

foreach (var rectangle in FindVisualChildren<Rectangle>(this))
{
  if (rectangle.Name == "rectangleBarChart")
  {
    /*   Your code here  */
  }
}

回答by H.B.

Do notdo it. If you need to change something in a DataTemplatethen bind the respective properties and modify the underlying data. Also i would recommend binding the Button.Commandto an ICommandon your data/view-model (see MVVM) instead of using events, then you are in the right context already and the view does not need to do anything.

千万不能这样做。如果您需要在 a 中更改某些内容,DataTemplate则绑定相应的属性并修改基础数据。此外,我建议将 绑定Button.CommandICommand您的数据/视图模型(请参阅 MVVM)而不是使用事件,然后您已经处于正确的上下文中并且视图不需要执行任何操作。