Hi I'm trying to use a template item into the code behind c#
嗨,我正在尝试将模板项用于c#背后的代码
I'm trying to access the PictureBox which is WindowsFormHost. And then set it to what I want.
我正在尝试访问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>
Whenever I use this style in C# I do :
每当我在C#中使用这种风格时,我会:
var listboxitem = new ToggleButton();
listboxitem.Style = (Style)FindResource("ScreenListBox");
VideoListBox.Items.Add(listboxitem);
//Here I want to be able to use the PictureBox in ScreenListBox.
//这里我希望能够在ScreenListBox中使用PictureBox。
To pass it to another function.
将它传递给另一个函数。
I've been trying with Template.FindName and it didn't work.
我一直在尝试使用Template.FindName,它不起作用。
Could anyone have a look and try to bring me into the right direction.
任何人都可以看看,并试图让我走向正确的方向。
Thanks.
谢谢。
1 个解决方案
#1
3
Hi I guess only one way to implement this will be to create a custom Control which you can inherit form ToggleButton. Somewhere you should define control template for your control (like generic.xaml or App.xaml). In your custom control you should override OnApplyTemplate method, where you can get access at least to the WindowsFormsHost (but maybe to PictureBox as well) with FindName method.
嗨,我想只有一种实现方法是创建一个自定义控件,你可以继承ToggleButton。在某处你应该为你的控件定义控件模板(如generic.xaml或App.xaml)。在自定义控件中,您应该重写OnApplyTemplate方法,在该方法中,您可以使用FindName方法至少访问WindowsFormsHost(但也可以访问PictureBox)。
You can also try to use your code, before you will try to get access to PictureBox - try to invoke ApplyTemplate, maybe this will force ToggleButton to load everything and it will give you access to it's children.
在尝试访问PictureBox之前,您也可以尝试使用您的代码 - 尝试调用ApplyTemplate,这可能会强制ToggleButton加载所有内容,它会让您访问它的子项。
#1
3
Hi I guess only one way to implement this will be to create a custom Control which you can inherit form ToggleButton. Somewhere you should define control template for your control (like generic.xaml or App.xaml). In your custom control you should override OnApplyTemplate method, where you can get access at least to the WindowsFormsHost (but maybe to PictureBox as well) with FindName method.
嗨,我想只有一种实现方法是创建一个自定义控件,你可以继承ToggleButton。在某处你应该为你的控件定义控件模板(如generic.xaml或App.xaml)。在自定义控件中,您应该重写OnApplyTemplate方法,在该方法中,您可以使用FindName方法至少访问WindowsFormsHost(但也可以访问PictureBox)。
You can also try to use your code, before you will try to get access to PictureBox - try to invoke ApplyTemplate, maybe this will force ToggleButton to load everything and it will give you access to it's children.
在尝试访问PictureBox之前,您也可以尝试使用您的代码 - 尝试调用ApplyTemplate,这可能会强制ToggleButton加载所有内容,它会让您访问它的子项。