繁体   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