简体   繁体   English

为什么我的命名WPF形状不受代码背后的影响?

[英]Why is my Named WPF Shape Inaccesible from Code Behind?

I have an Ellipse in my WPF application. 我的WPF应用程序中有一个Ellipse I want to change the colour of its outline whenever it is double clicked. 我想在双击时更改其轮廓的颜色。 I found this (old) tutorial about making this work by using the available MouseDown event and checking for a ClickCount of two in the event handler. 我找到了这个 (旧)教程,通过使用可用的MouseDown事件并在事件处理程序中检查两个ClickCount来完成此工作。 This is the simplest solution to my problem and I'd like to try and get this to work before creating an empty button Control Template . 这是我的问题最简单的解决方案,我想在创建一个空按钮控制模板之前尝试让它工作。
However, I'm unable to find the clicked ellipse in my code behind file. 但是,我无法在我的代码隐藏文件中找到单击的椭圆。 Supposedly this works in the tutorial, but I'm wondering if I'm missing anything. 据说这可以在教程中使用,但我想知道我是否遗漏了任何东西。
Here's the code that contains the ellipse. 这是包含椭圆的代码。 It is the 3rd column of a grid: 它是网格的第3列:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="3">
     <StackPanel Orientation="Vertical" Margin="3,1" Background="GhostWhite">
        <ItemsControl Name="FlowLinkItems" ItemsSource="{Binding FlowLinkList}">
           <ItemsControl.ItemTemplate>
              <DataTemplate>
                 <Grid Height="40">
                    <Ellipse Name="FlowLinkEllipse" Stroke="BlueViolet" Height="38" VerticalAlignment="Center" MouseDown="Ellipse_MouseDown"/>
                    <TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Message}"></TextBlock>
                 </Grid>
              </DataTemplate>
           </ItemsControl.ItemTemplate>
        </ItemsControl>
     </StackPanel>
  </ScrollViewer>

In the tutorial, the code behind method worked like this: 在本教程中,代码隐藏方法的工作方式如下:

private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
  {
     if (e.ClickCount == 2)
     {
        FlowLinkEllipse.Stroke = "Red";
     }
  }

And the error I'm seeing is: 我看到的错误是:

The name 'FlowLinkEllipse' does not exist in the current context “FlowLinkEllipse”名称在当前上下文中不存在

If this method is not possible I'm open to suggestions that are as simple as possible (I'm still new to WPF and the only thing my app will handle is this double click). 如果这种方法不可行,我会接受尽可能简单的建议(我还是WPF新手,我的应用程序唯一能处理的就是双击)。
Note: I do have this line in my code behind and it works fine. 注意:我的代码后面有这一行,它运行正常。
FlowLinkItems.MouseLeftButtonUp += FlowLinkItems_MouseLeftButtonUp;

As @Magus noted, you can't reference an item from code-behind, that is inside a DataTemplate . 正如@Magus所说,你不能从代码隐藏中引用一个项目,即DataTemplate That should be no problem here, though: sender will contain a reference to the ellipse: 这应该没有问题,但是: sender将包含对椭圆的引用:

private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
   if (ellipse as sender == null || e.ClickCount < 2)
      return;

   var ellipse = (Ellipse)sender;
   ellipse.Stroke = System.Windows.Media.Brushes.Red;
}

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

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