簡體   English   中英

在XAML中使用來自自定義控件的C#變量(Silverlight)

[英]Using a C# variable from a custom control in xaml (silverlight)

關於這個主題,我已經看了很多其他的帖子,但是現在我花了一天多的時間,試圖使一些事情起作用,但是我會嘗試在這里提出問題,希望您能幫助我。

我想做一個自定義控件,可以在屬性菜單中添加值,在我的項目中添加控件的地方,每次都需要一個新的控件。

我需要在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”的對象

希望您能夠幫助我。

我懷疑問題出在這一行

    <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應用於這樣的屬性。 如果要使用綁定設置屬性,則它必須是依賴項屬性 相反,它應類似於以下內容:

    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代碼片段為您生成了大部分propdp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM