2017-08-01 67 views
0

我的問題訪問ScaleTransform這是在ListBox從代碼隱藏

我想一個ScaleTransform添加到我的畫布,並試圖在我看來後面的代碼加入對它的代碼。但是,雖然VisualStudio向我顯示訪問者,但當我嘗試在後面的代碼中使用它時會引發錯誤。

我的列表框看起來是這樣的:

<Grid> 
    <cc:ListBoxNoDragSelection ItemsSource="{Binding MainModel.Rectangles}" Background="{DynamicResource BG}" SelectedItem="{Binding Selected}" 
           BorderThickness="0" x:Name="ListBoxNoDragSelection"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas x:Name="MockupCanvas" MouseWheel="MockupCanvas_OnMouseWheel" Background="Transparent"> 
        <Canvas.RenderTransform> 
         <ScaleTransform x:Name="St"/> 
        </Canvas.RenderTransform> 
        <Canvas.ContextMenu> 
         <ContextMenu> 
          <MenuItem Header="New rectangle" Command="{Binding AddNewRectangleToCollectionCommand}"/> 
          <MenuItem Header="Delete selected rectangle" Command="{Binding TryDeleteRectangleFromCollectionCommand}"/> 
         </ContextMenu> 
        </Canvas.ContextMenu> 
       </Canvas> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Canvas.Left" Value="{Binding X}"/> 
       <Setter Property="Canvas.Top" Value="{Binding Y}"/> 
       <Setter Property="Canvas.ZIndex" Value="{Binding Z}"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <DockPanel Height="Auto" Width="{Binding Width}"> 
        <Label DockPanel.Dock="Top" Content="{Binding Name}" Foreground="{DynamicResource Foreground}" IsHitTestVisible="False"/> 
        <Rectangle Width="{Binding Width}" Height="{Binding Height}" DockPanel.Dock="Bottom" 
           Fill="Transparent" Stroke="White" StrokeThickness="3" RadiusX="10" RadiusY="10" /> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </cc:ListBoxNoDragSelection> 
</Grid> 

最重要的部分是:

<Canvas x:Name="MockupCanvas" MouseWheel="MockupCanvas_OnMouseWheel" Background="Transparent"> 
        <Canvas.RenderTransform> 
         <ScaleTransform x:Name="St"/> 
        </Canvas.RenderTransform> 
        <Canvas.ContextMenu> 
         <ContextMenu> 
          <MenuItem Header="New rectangle" Command="{Binding AddNewRectangleToCollectionCommand}"/> 
          <MenuItem Header="Delete selected rectangle" Command="{Binding TryDeleteRectangleFromCollectionCommand}"/> 
         </ContextMenu> 
        </Canvas.ContextMenu> 
       </Canvas> 

代碼隱藏:

private void MockupCanvas_OnMouseWheel(object sender, MouseWheelEventArgs e) 
    { 

     if (e.Delta > 0) 
     { 
      St.ScaleX *= ScaleRate; 
      St.ScaleY *= ScaleRate; 
     } 
     else 
     { 
      St.ScaleX /= ScaleRate; 
      St.ScaleY /= ScaleRate; 
     } 
    } 

Visual Studio中不斷告訴我, St不在當前可用的上下文中。

我在做什麼錯?

回答

1

你可以只讓ScaleTransform了畫布,而不是通過名字來訪問它的:

private void MockupCanvas_OnMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    var scaleTransformation = (sender as Canvas)?.RenderTransform as ScaleTransform; 

    if (scaleTransformation == null) 
     return; 
    if (e.Delta > 0) 
    { 
     scaleTransformation.ScaleX *= ScaleRate; 
     scaleTransformation.ScaleY *= ScaleRate; 
    } 
    else 
    { 
     scaleTransformation.ScaleX /= ScaleRate; 
     scaleTransformation.ScaleY /= ScaleRate; 
    } 
}