简体   繁体   English

在XAML中使用来自自定义控件的C#变量(Silverlight)

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

I have looked aa lot of other post, regarding this topic, but now i have spent more then a day, trying to get something to work, but i will try to aske the qustion here, and hope you can help me. 关于这个主题,我已经看了很多其他的帖子,但是现在我花了一天多的时间,试图使一些事情起作用,但是我会尝试在这里提出问题,希望您能帮助我。

I want to make a custom control where i can add values in the property menu, where i add the control inside my project, it need to be something new each time. 我想做一个自定义控件,可以在属性菜单中添加值,在我的项目中添加控件的地方,每次都需要一个新的控件。

And the info i need to a resource control in xaml, but i can not bind the strings from C# to what i need in xaml: 我需要在xaml中进行资源控制的信息,但是我无法将C#的字符串绑定到xaml中所需的内容:

The C# code: 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; }
}
}

Where i want to use "NavPage" and "DesciptionPageName" to add then in xaml. 在我想使用“ NavPage”和“ DesciptionPageName”添加的地方,然后在xaml中添加。

The xaml code: 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>

I can build the project, but when i add the custom control, i get this error: 我可以构建项目,但是当我添加自定义控件时,出现此错误:

object of type 'system.windows.data.binding' cannot be converted to type 'system.string' 类型为“ system.windows.data.binding”的对象无法转换为类型为“ system.string”的对象

Hope you can help me. 希望您能够帮助我。

I suspect that the problem is in this line 我怀疑问题出在这一行

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

You haven't provided the source of your TranslatableCPNavItem class, so I'm going to have to make an assumption based on the code of another class you've provided above. 您尚未提供TranslatableCPNavItem类的源,因此我将不得不根据您在上面提供的另一个类的代码进行假设。 I'm guessing that the Description property within this class is implemented as a 'simple' property, such as 我猜想此类中的Description属性被实现为“简单”属性,例如

    public string Description { get; set; }

You can't apply a Binding to a property such as this. 您不能将Binding应用于这样的属性。 If you want a property to be set using a binding, it needs to be a dependency property . 如果要使用绑定设置属性,则它必须是依赖项属性 It should instead look something like the following: 相反,它应类似于以下内容:

    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);

This does look like quite a lot to type in for each property that you want to use with bindings, but there is a propdp code snippet that generates most of it for you. 对于要与绑定一起使用的每个属性,确实要输入很多内容,但是有一个propdp代码片段为您生成了大部分propdp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM