2013-04-03 111 views
1

嗨,我試圖用一個模板項目進入後面的代碼C#如何通過代碼在WPF獲取樣式的特定元素C#

我試圖訪問的PictureBox這是WindowsFormHost。 然後將其設置爲我想要的。

<Style x:Key="ScreenListBox" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Padding" Value="2,0,0,0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true" Margin="-0.001,0" Background="#FFABABAB" Width="120" Height="117"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="stackPanel"> 
              <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
             </DoubleAnimationUsingKeyFrames> 
             <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBlock"> 
              <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
             </DoubleAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="SelectionStates"> 
           <VisualState x:Name="Unselected"/> 
           <VisualState x:Name="Selected"/> 
           <VisualState x:Name="SelectedUnfocused"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="ValidationStates"> 
           <VisualState x:Name="Valid"/> 
           <VisualState x:Name="InvalidFocused"/> 
           <VisualState x:Name="InvalidUnfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <StackPanel Orientation="Vertical" Name="TestStackPanel" d:LayoutOverrides="Height"> 
          <Grid Margin="3" Height="85" Name="TestGrid" Background="White"> 
           <WindowsFormsHost Name="wrapper" Margin="0,0,0,0" Background="{x:Null}"> 
            <Forms:PictureBox Name="Picture"></Forms:PictureBox> 
           </WindowsFormsHost> 
           <StackPanel x:Name="stackPanel" Margin="0" Width="Auto" Opacity="0"> 
            <DockPanel Margin="0" Width="Auto" Height="45"> 
             <Rectangle Fill="#FFD8D8D8" Margin="0" Width="16" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FFEBE77A" Margin="0" Width="17" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FF60CDE9" Margin="0" Width="16" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FF60E98E" Margin="0" Width="16" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FFC65F9C" Margin="0" Width="16" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FFC43232" Margin="0" Width="17" HorizontalAlignment="Left"/> 
             <Rectangle Fill="#FF0A37A5" Margin="0" Width="16" HorizontalAlignment="Left"/> 
            </DockPanel> 
            <DockPanel HorizontalAlignment="Stretch" Height="5" Width="Auto"> 
             <Rectangle Fill="#FF0A37A5" Margin="0" Width="16" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="Black" Margin="0" Width="16" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="#FFC65F9C" Margin="0" Width="17" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="Black" Margin="0" Width="16" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="#FF60CDE9" Margin="0" Width="16" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="Black" Margin="0" Width="16" HorizontalAlignment="Left" Height="Auto"/> 
             <Rectangle Fill="#FFD8D8D8" Margin="0" Width="17" HorizontalAlignment="Left" Height="Auto"/> 
            </DockPanel> 
            <DockPanel HorizontalAlignment="Stretch" Height="15" VerticalAlignment="Bottom"> 
             <TextBlock x:Name="textBlock" TextWrapping="Wrap" Opacity="0" FontWeight="Bold" Width="60" TextAlignment="Center" FontSize="9.333"/> 
            </DockPanel> 

           </StackPanel> 

          </Grid> 
          <Grid Margin="5,0" Height="20" VerticalAlignment="Bottom"> 
           <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Bottom" Margin="15,0" HorizontalAlignment="Center"> 
            <ContentPresenter.Resources> 
             <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/> 
            </ContentPresenter.Resources> 
           </ContentPresenter> 
           <Button Content="Button" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Right" Style="{DynamicResource Edit}"/> 
           <Path x:Name="path" Data="F1M29.2007,72.9482L25.9947,75.2372L25.9947,73.7922L18.5627,73.7922L18.5627,78.9302L25.9947,78.9302L25.9947,77.4862L29.2007,79.7752z" Fill="#FF231F20" Height="9.327" Stretch="Fill" Width="14.534" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" Opacity="0"/> 
           <Rectangle x:Name="rectangle" Fill="#FF231F20" Margin="0" Width="20" Height="20" Opacity="0" VerticalAlignment="Top" d:LayoutOverrides="Width, Height, VerticalMargin" HorizontalAlignment="Left"/> 
          </Grid> 
         </StackPanel> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsChecked" Value="true"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
          <Setter Property="Opacity" TargetName="path" Value="1"/> 
          <Setter Property="Background" TargetName="Bd" Value="#FF26A1E5"/> 
         </Trigger> 
         <Trigger Property="Selector.IsSelected" Value="False"/> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

每當我在C#中使用這種風格我做的:

var listboxitem = new ToggleButton(); 

listboxitem.Style = (Style)FindResource("ScreenListBox"); 

VideoListBox.Items.Add(listboxitem); 

//在這裏,我希望能夠用圖片框在ScreenListBox。

將其傳遞給另一個函數。

我一直在嘗試Template.FindName,它沒有工作。

任何人都可以看一看,並嘗試將我帶入正確的方向。

謝謝。

+0

Uff ....我沒有看到那裏有一個winforms的東西...那應該做什麼? – 2013-04-03 22:04:32

+0

嗨HighCore,winform是能夠得到一個控件句柄。 然後將它傳遞給一些pvinvoke的東西,以便能夠在該控件中顯示win32的東西 – Jonathan 2013-04-04 13:31:25

回答

3

嗨我想只有一種方法來實現這將是創建一個自定義控件,你可以繼承形式ToggleButton。某處應該爲控件定義控件模板(如generic.xaml或App.xaml)。在您的自定義控件中,您應該重寫OnApplyTemplate方法,您可以在其中使用FindName方法至少訪問WindowsFormsHost(但也可能用於PictureBox)。

在嘗試訪問PictureBox之前,您也可以嘗試使用您的代碼 - 嘗試調用ApplyTemplate,也許這會強制ToggleButton加載所有內容,並且會讓您訪問它的子項。

+0

謝謝,這正是我所需要的。 – Jonathan 2013-04-04 14:20:58

相關問題