2012-09-12 67 views
0

我正在創建一系列ControlTemplates,以便從某些字符串顯示在WPF控件上。我使用此代碼:字符串XAML ControlTemplate

string theTemplate = @"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> 
       <Grid Name=""RootElement"" RenderTransformOrigin=""0.5,0.5"" > 
        <Grid.RenderTransform> 
         <ScaleTransform ScaleX=""1"" ScaleY=""1"" /> 
        </Grid.RenderTransform> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup Name=""CommonStates""> 
          <VisualState Name=""Normal""> 
           <Storyboard> 
            <DoubleAnimation BeginTime=""00:00:00"" 
                   Storyboard.TargetName=""RootElement"" 
                   Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleX)"" 
                   To=""1"" Duration=""0:0:0.1"" /> 
            <DoubleAnimation BeginTime=""00:00:00"" 
                   Storyboard.TargetName=""RootElement"" 
                   Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleY)"" 
                   To=""1"" Duration=""0:0:0.1"" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState Name=""MouseOver""> 
           <Storyboard> 
            <DoubleAnimation BeginTime=""00:00:00"" 
                   Storyboard.TargetName=""RootElement"" 
                   Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleX)"" 
                   To=""1.5"" Duration=""0:0:0.1"" /> 
            <DoubleAnimation BeginTime=""00:00:00"" 
                   Storyboard.TargetName=""RootElement"" 
                   Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleY)"" 
                   To=""1.5"" Duration=""0:0:0.1"" /> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
       <Ellipse Height=""{Binding Symbol.Size}"" Width=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black""> 

       </Ellipse> 
      </Grid> 
     </ControlTemplate>"; 
System.IO.StringReader stringReader = new System.IO.StringReader(theTemplate); 
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader); 
ControlTemplate ct = (ControlTemplate)System.Windows.Markup.XamlReader.Load(reader); 

這一切都很好。問題是,而不是使用橢圓,我試着使用我的自定義形狀。在常規的WPF窗口,我用這個參考:

xmlns:custom="clr-namespace:WpfApplication2" 

,然後改爲橢圓的,我可以這樣做:

<custom:Square Size=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black"" RotationAngle=""0""> 
</custom:Square> 

現在,當我再補充一點行字符串「theTemplate」它現在的工作。我想原因是因爲我沒有在引用我曾嘗試加入不同的地方引用,如:。

<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:custom="clr-namespace:WpfApplication2" > 

,並沒有工作。我也試過:

<custom:Square Size=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black"" RotationAngle=""0"" xmlns:custom="clr-namespace:WpfApplication2" > 
</custom:Square> 

而且也不喜歡它。

Sooo,有什麼建議嗎?我如何參考我的自定義形狀?

謝謝!

回答

1

使用XamlReader時,通常需要完全限定您的CLR xmlns,即添加組件。

例如

xmlns:custom="clr-namespace:WpfApplication2;assembly=WpfApplication2"