C# 向windows phone上的自定义控件添加绑定
我已经为一个项目创建了一个自定义图像按钮。这是基于上提供的代码 我已经调整了图像按钮,以满足我的需要,它看起来很好的设计师。但是,我尝试使用DependencyProperty将TextBlock绑定到视图模型 按钮的内部看起来像这样C# 向windows phone上的自定义控件添加绑定,c#,windows-phone-8,windows-phone-8.1,C#,Windows Phone 8,Windows Phone 8.1,我已经为一个项目创建了一个自定义图像按钮。这是基于上提供的代码 我已经调整了图像按钮,以满足我的需要,它看起来很好的设计师。但是,我尝试使用DependencyProperty将TextBlock绑定到视图模型 按钮的内部看起来像这样 <StackPanel x:Name="Panel" Orientation="Horizontal" VerticalAlignment="Center"> <Image Margin="10,0,-10,0" x:Name="Imag
<StackPanel x:Name="Panel" Orientation="Horizontal" VerticalAlignment="Center">
<Image Margin="10,0,-10,0" x:Name="Image" Width="48" Height="48" Source="/Assets/Images/mappin.png"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Width="400" x:Name="txtHeading" Text="Mooo" Grid.Row="0" TextAlignment="Center" Margin="20,0,-10,0" FontFamily="/TfL.CongestionCharge.WindowsPhone;component/Assets/Fonts/NJFont-Book.ttf#NJFont Book" />
</Grid>
</StackPanel>
</StackPanel>
public static readonly DependencyProperty TextDependencyProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(UserControl),
new PropertyMetadata(null));
public string Text
{
get
{
return GetValue(TextDependencyProperty) as string;
}
set
{
SetValue(TextDependencyProperty, txtHeading.GetType().GetProperty("System.String").GetValue(txtHeading, null));
}
}
(我无法从System.Windows.Bindable转换为System.String错误最初,此代码将编译,但在setter中出现null ref.异常)
如果我尝试绑定到ICommand,我是否需要创建DependencyProperty,或者我是否可以使用
private ICommand command;
public ICommand Command
{
get
{
return command;
}
set
{
command = value;
command.Execute(value);
}
}
将您的setter改为“正常”setter:
SetValue(TextDependencyProperty, txtHeading.GetType().GetProperty("System.String").GetValue(txtHeading, null));
使用以下命令:
SetValue(TextDependencyProperty, value);
为您的UserControl
命名:
<UserControl ... x:Name="thisControl">
希望这能有所帮助。将您的setter改为“普通”setter:
SetValue(TextDependencyProperty, txtHeading.GetType().GetProperty("System.String").GetValue(txtHeading, null));
使用以下命令:
SetValue(TextDependencyProperty, value);
为您的UserControl
命名:
<UserControl ... x:Name="thisControl">
希望这有帮助。的确如此。现在只有两个问题-图像不显示,绑定文本也不显示:(这很奇怪。此更改应该不会影响图像。您能否提供
用户控件的完整XAML
?D'oh missing/before Assets。但不确定未绑定的文本:(在提供的代码中,控件中没有依赖项属性。也没有从属性定义到文本块和图像的绑定。第81行和第87行有属性。绑定来自主视图模型(例如绑定VrmLookupCommand)是的。现在只有两个问题-图像不显示,绑定文本也不显示:(这很奇怪。此更改应该不会影响图像。您能否提供UserControl
?D'oh missing/before资产的完整XAML。但不确定文本是否未绑定:(在提供的代码中,控件中没有依赖项属性。也没有从属性定义到文本块和图像的绑定。第81行和第87行有属性。绑定来自主视图模型(例如绑定VrmLookupCommand)