抱歉,如果我重复这个话题。但是,以前的解决方案都没有对我有用。我试图为ContentControl创建一个teplate,当用户按下按钮时,它会自动生成。
Xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:WpfApplication1"
Title="MainWindow" Height="371" Width="556" Closing="Window_Closing" KeyDown="Window_KeyDown">
<Grid Height="325" Width="518">
<Grid.RowDefinitions>
<RowDefinition Height="172*" />
<RowDefinition Height="153*" />
</Grid.RowDefinitions>
<MediaElement Height="21" HorizontalAlignment="Left" Name="mediaElement1" VerticalAlignment="Top" Width="506" MediaOpened="mediaElement1_MediaOpened" Margin="0,30,0,0" UnloadedBehavior="Manual" LoadedBehavior="Manual" Stretch="None"/>
<ListBox Height="Auto" HorizontalAlignment="Stretch" Margin="359,151,0,0" Name="listBox1" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding}" Grid.RowSpan="2" />
<ListBox Height="Auto" HorizontalAlignment="Stretch" Margin="0,151,368,0" Name="listBox2" VerticalAlignment="Stretch" ItemsSource="{Binding}" Grid.RowSpan="2" />
<Button Content="Browse" Height="23" HorizontalAlignment="Left" Margin="359,122,0,0" Name="Enviroment" VerticalAlignment="Top" Width="75" Click="Enviroment_Click" />
<Button Content="Enviroment" Height="23" HorizontalAlignment="Left" Margin="12,122,0,0" Name="Browse" VerticalAlignment="Top" Width="75" Click="Browse_Click" />
<Button Content="Play" Height="23" HorizontalAlignment="Left" Margin="156,79,0,0" Name="Play" VerticalAlignment="Top" Width="75" Click="Play_Click" />
<Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="245,79,0,0" Name="Stop" VerticalAlignment="Top" Width="75" Click="Stop_Click" />
<Slider Height="23" HorizontalAlignment="Left" Margin="0,50,0,0" Name="slider1" VerticalAlignment="Top" Width="506" Thumb.DragStarted="slider1_DragStarted" Thumb.DragCompleted="slider1_DragCompleted" MouseDown="slider1_MouseDown" />
<Canvas Height="Auto" HorizontalAlignment="Stretch" Margin="156,151,165,0" Name="canvas1" VerticalAlignment="Stretch" Opacity="1" Background="White" OpacityMask="Black" Grid.RowSpan="2">
<Canvas.Resources>
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
<ControlTemplate x:Key="DesignerItemTemplate" TargetType="Control">
<Grid>
<s:MoveThumb Template="{StaticResource MoveThumbTemplate}"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Cursor="SizeAll"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
</ControlTemplate>
</Canvas.Resources>
<ContentControl Name="contentControl1" Width="26" Height="25" Canvas.Left="79" Canvas.Top="70"
Template="{StaticResource DesignerItemTemplate}">
<Ellipse Canvas.Left="86" Canvas.Top="70" Height="19" Name="ellipse1" Stroke="Black" Width="19" Fill="Red" IsHitTestVisible="False" />
</ContentControl>
<ContentControl Name="contentControl2" Width="26" Height="26" Canvas.Left="140" Canvas.Top="26"
Template="{StaticResource DesignerItemTemplate}">
<Ellipse Canvas.Left="140" Canvas.Top="33" Fill="#FF93FF14" Height="19" Name="ellipse2" Stroke="Black" Width="19" IsHitTestVisible="False" />
</ContentControl>
</Canvas>
<TextBlock Height="23" HorizontalAlignment="Right" Margin="0,123,236,0" Name="textBlock1" Text="/ 200" VerticalAlignment="Top" Width="37" />
<TextBlock Height="23" HorizontalAlignment="Right" Margin="0,123,301,0" Name="textBlock2" Text="/ 162" VerticalAlignment="Top" Width="39" />
<TextBlock Height="22" Margin="156,123,334,0" Name="textBlock3" Text="" VerticalAlignment="Top" />
<TextBlock Height="23" Margin="0,123,257,0" Name="textBlock4" Text="" VerticalAlignment="Top" HorizontalAlignment="Right" Width="38" />
<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="443,122,0,0" Name="Load" VerticalAlignment="Top" Width="63" Click="Load_Click" />
</Grid>
和班级的一部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using rayav_csharp;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
string template = "<ControlTemplate x:Key=\"MoveThumbTemplate\" TargetType=\"{x:Type s:MoveThumb}\">" +
"<Rectangle Fill=\"Transparent\"/>" +
"</ControlTemplate>";
ParserContext parser = new ParserContext();
parser.XmlnsDictionary.Add("","http://schemas.microsoft.com/winfx/2006/xaml/presentation");
parser.XmlnsDictionary.Add("x","http://schemas.microsoft.com/winfx/2006/xaml");
parser.XmlnsDictionary.Add("s","clr-namespace:WpfApplication1");
control.Template = (ControlTemplate)XamlReader.Parse(template, parser);
public class MoveThumb : Thumb
{
public MoveThumb()
{
DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
}
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Control item = this.DataContext as Control;
if (item != null)
{
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);
Canvas.SetLeft(item, left + e.HorizontalChange);
Canvas.SetTop(item, top + e.VerticalChange);
}
}
}
任何人都可以提供帮助? 问候