简体   繁体   English

在Windows Phone上向自定义控件添加绑定

[英]Adding binding to a custom control on windows phone

I have created a custom image button for a project. 我为项目创建了一个自定义图像按钮。 This is based on the code provided at https://code.msdn.microsoft.com/windowsapps/Custom-Image-Button-in-4b300b62 . 这基于https://code.msdn.microsoft.com/windowsapps/Custom-Image-Button-in-4b300b62提供的代码。

I've adjusted the image button to suit my needs and it looks fine in the designer. 我已经调整了图像按钮以适合我的需求,并且在设计器中看起来不错。 However, I'm trying to bind the textblocks to a view model using a DependencyProperty. 但是,我试图使用DependencyProperty将文本块绑定到视图模型。

The guts of the button looks like this 按钮的内胆看起来像这样

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

Nothing amazing, but does what I need. 没什么了不起的,但是我需要什么。

The code behind for the TextBlock dependency looks like this TextBlock依赖项的背后代码如下所示

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

(I was getting could not convert from a System.Windows.Bindable to System.String error originally, this code will compile but gives a null ref. exception in the setter) (我本来无法从System.Windows.Bindable转换为System.String错误,此代码将编译,但在setter中提供空引用异常。)

Am I going about this the correct way and do I need to create a DependencyProperty if I try to bind to an ICommand or can I just use 我要使用正确的方法吗?如果我尝试绑定到ICommand,还是需要使用DependencyProperty?

private ICommand command;
public ICommand Command
    {
        get
        {
            return command;
        }

        set
        {
            command = value;
            command.Execute(value);
        }
    }

Change your setter to be "normal" one, instead: 将您的设置员更改为“正常”设置,而不是:

SetValue(TextDependencyProperty, txtHeading.GetType().GetProperty("System.String").GetValue(txtHeading, null));

Use this: 用这个:

SetValue(TextDependencyProperty, value);

Give your UserControl a name: 为您的UserControl命名:

<UserControl ... x:Name="thisControl">

In your TextBlock use a binding to your control property: TextBlock使用对控件属性的绑定:

Text="{Binding Text, ElementName=thisControl}"

Hope this helps. 希望这可以帮助。

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

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