2011-11-19 101 views
0

我看過很多關於此主題的其他帖子,但現在我花了一天多的時間嘗試獲取某些內容,但我會嘗試在這裏提出質疑,並希望你能幫助我。在xaml中使用自定義控件中的C#變量(silverlight)

我想製作一個自定義控件,我可以在屬性菜單中添加值,其中我在項目中添加控件,每次都需要新的東西。

以及info我需要在XAML資源控制,但我不能從C#中的字符串綁定到我在XAML需要:

的C#代碼:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Windows.Threading; 
using WFSilverlight.Shared; 
using WFSilverlight.Core; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using BG_CalgaryNC_11611.Controls; 

namespace BG_CalgaryNC_11611 
{ 
public partial class BG_Calgary_Text_Nav : UserControl 
{ 
    //public string NavPage = String.Empty; 
    [Category("BG")] 
    public string PageToNavigate { get; set; } 

    [Category("BG")] 
    public string DesciptionPageName { get; set; } 

    [Category("BG")] 
    public string DisplayName { get; set; } 

    public BG_Calgary_Text_Nav() 
    { 
     InitializeComponent(); 

     NavText.Text = DisplayName; 
     NavPage = "BG_CalgaryNC_11611_" + PageToNavigate; 

     //this.DataContext = this; 
     //NavText.Resources.Add("navPageResourse", NavPage); 
     //NavText.Resources.Add("displayNameResourse", DesciptionPageName); 

    } 

    public string NavPage { get; set; } 
} 
} 

,我想使用「NavPage」和「DesciptionPageName」在xaml中添加。

的XAML代碼:

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:BG_CalgaryNC_11611_Controls="clr-namespace:BG_CalgaryNC_11611.Controls" 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
mc:Ignorable="d" 

x:Class="BG_CalgaryNC_11611.BG_Calgary_Text_Nav" 
d:DesignWidth="640" d:DesignHeight="480"> 

<Grid x:Name="LayoutRoot" Cursor="Hand"> 
    <Viewbox HorizontalAlignment="Right" VerticalAlignment="Top"> 
     <TextBlock x:Name="NavText" TextWrapping="Wrap" FontSize="10.667" FontWeight="Bold" Text="TC9-21" Height="16" Width="43"> 
      <TextBlock.Resources> 
       <BG_CalgaryNC_11611_Controls:TranslatableCPNavItem Description="{Binding NavPage, Mode=OneWay}" Source="{Binding DesciptionPageName, Mode=OneWay}" x:Key="target1"/> 
      </TextBlock.Resources> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="MouseLeftButtonUp"> 
        <i:InvokeCommandAction Command="{Binding NavigatToCommand, Mode=OneWay}" CommandParameter="{StaticResource target1}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
      <TextBlock.Foreground> 
       <SolidColorBrush Color="{StaticResource BG_ConnectionArrow_Color}"/> 
      </TextBlock.Foreground></TextBlock> 
    </Viewbox> 
</Grid> 

我可以生成項目,但是當我添加自定義的控制,我得到這個錯誤:類型的

對象system.windows。 data.binding'無法轉換爲類型'system.string'

希望你能幫助我。

回答

1

我懷疑,這個問題是在這條線

<BG_CalgaryNC_11611_Controls:TranslatableCPNavItem Description="{Binding NavPage, Mode=OneWay}" Source="{Binding DesciptionPageName, Mode=OneWay}" x:Key="target1"/> 

您沒有提供您的TranslatableCPNavItem類的源,所以我不得不做出基於另一個類的代碼假設你已經在上面提供了。我猜,這個類中的Description屬性是作爲一個「簡單」的屬性來實現,如

public string Description { get; set; } 

不能應用Binding的財產,如這一點。如果你想要使用綁定來設置屬性,它需要是dependency property。這應該不是看起來像如下:

public string Description 
    { 
     get { return (string)GetValue(DescriptionProperty); } 
     set { SetValue(DescriptionProperty, value); } 
    } 

    public static readonly DependencyProperty DescriptionProperty = 
     DependencyProperty.Register("Description", 
            typeof(string), 
            typeof(TranslateableCPNavItem), 
            null); 

這看起來像不少在要使用綁定使用的每個屬性類型,但有產生大部分爲propdp代碼片段您。

相關問題