Steffen Beth
Steffen Beth

Reputation: 1

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

I have looked a a 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:

The C# code:

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.

The xaml code:

    <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'

Hope you can help me.

Upvotes: 0

Views: 1108

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64949

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. I'm guessing that the Description property within this class is implemented as a 'simple' property, such as

    public string Description { get; set; }

You can't apply a Binding to a property such as this. 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.

Upvotes: 1

Related Questions