在xaml(silverlight)中使用自定义控件中的C#变量
关于这个话题,我已经看了很多其他的帖子,但是现在我已经花了一天多的时间,试图找到一些有用的东西,但是我会尝试在这里提问,希望你能帮助我 我想做一个自定义控件,我可以在属性菜单中添加值,在项目中添加控件,每次都需要一些新的东西 在xaml中,我需要一个资源控件,但我不能将C#中的字符串绑定到我在xaml中需要的内容: C#代码: 我想使用“NavPage”和“DescriptionPageName”添加到xaml中 xaml代码:在xaml(silverlight)中使用自定义控件中的C#变量,c#,xaml,custom-controls,C#,Xaml,Custom Controls,关于这个话题,我已经看了很多其他的帖子,但是现在我已经花了一天多的时间,试图找到一些有用的东西,但是我会尝试在这里提问,希望你能帮助我 我想做一个自定义控件,我可以在属性菜单中添加值,在项目中添加控件,每次都需要一些新的东西 在xaml中,我需要一个资源控件,但我不能将C#中的字符串绑定到我在xaml中需要的内容: C#代码: 我想使用“NavPage”和“DescriptionPageName”添加到xaml中 xaml代码: <UserControl xmlns="http:/
<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"/>
不能将绑定
应用于此类属性。如果希望使用绑定设置属性,则该属性必须是。相反,它应该如下所示:
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
代码片段为您生成了大部分内容
public string Description { get; set; }
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);