Question

I am working on a Silverlight for Windows Embedded project. I defined a custom user control which consists of a TextBlock control and an image control. I want to specify different text in different instance of the custom control. So I did the following in Expression Blend: In Blend code-behind (C#) UserControl1.xaml.cs I defined and registered a DependencyProperty and set DataContext of the TextBlock:

namespace WindowsEmbeddedSilverlightApplication1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            ItemText.DataContext = this;
        }

        public String MyText
        {
            get { return (String)GetValue(MyTextProperty); }
            set { SetValue(MyTextProperty, value); }
        }

        public static readonly DependencyProperty MyTextProperty =
            DependencyProperty.Register("MyText", typeof(String), typeof(UserControl1), null);
    }
}

In UserControl1.xaml:

<UserControl
    ......
    x:Class="WindowsEmbeddedSilverlightApplication1.UserControl1"
    d:DesignWidth="196" d:DesignHeight="85">

    <Grid x:Name="LayoutRoot">
        <Image x:Name="ItemImage" Margin="0,0,90,0"/>
        <TextBlock x:Name="ItemText" HorizontalAlignment="Right" Width="68" Text="{Binding MyText}" TextWrapping="Wrap" Height="23" VerticalAlignment="Bottom"/>
    </Grid>
</UserControl>

To use the custom user control in MainPage.xaml:

<UserControl
    ......
    xmlns:local="clr-namespace:WindowsEmbeddedSilverlightApplication1"
    x:Class="WindowsEmbeddedSilverlightApplication1.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
        <local:UserControl1 HorizontalAlignment="Left" Margin="94,117,0,0" Width="196" Height="85" VerticalAlignment="Top" MyText="Text1"/>
        <local:UserControl1 HorizontalAlignment="Left" Margin="94,217,0,0" Width="196" Height="85" VerticalAlignment="Top" MyText="Text2"/>
    </Grid>
</UserControl>

So with these when I run the application in Expression Blend I am able to see the correct text displayed on the two instances of the custom user control.

Then I import the project into Visual Studio Silverlight for Windows Embedded Application. I read some posts mentioning that I have to redo the registeration. So I did the following:

In UserControl1.h:

    static HRESULT Register()
    {
        HRESULT hr = E_FAIL;
        hr = XRCustomUserControlImpl<UserControl1>::Register (__uuidof(UserControl1), L"UserControl1", L"clr-namespace:WindowsEmbeddedSilverlightApplication1");
        hr = RegisterDependencyProperties();
        return hr;
    }

    static HRESULT RegisterDependencyProperties();

    HRESULT SetMyText(WCHAR* pText);
    HRESULT GetMyText(BSTR* pbstrText);

public:
    static DEPENDENCY_PROPERTY_ID m_dpMyTextID;

In UserControl1.cpp:

HRESULT UserControl1::RegisterDependencyProperties()
{
    HRESULT hr = E_FAIL;
    IXRApplication* pApplication = NULL;

    XRDependencyPropertyMetaData dpm = XRDependencyPropertyMetaData();
    dpm.Size = sizeof(XRDependencyPropertyMetaData);
    dpm.pfnPropertyChangeNotification = NULL;
    dpm.pfnTypeConverter = NULL;
    dpm.pfnEnumerableCreation = NULL;

    XRValue defValue;
    defValue.vType = VTYPE_READONLY_STRING;
    defValue.pReadOnlyStringVal = L"Default";
    dpm.DefaultValue = defValue;

    hr = GetXRApplicationInstance(&pApplication);
    hr = pApplication->RegisterDependencyProperty(L"MyText", VTYPE_BSTR, ControlID(), &dpm, &m_dpMyTextID);

    pApplication->Release();

    return hr;
}

HRESULT UserControl1::SetMyText( WCHAR* pText )
{
    HRESULT hr = E_FAIL;

    XRValue xrValue;
    xrValue.vType = VTYPE_READONLY_STRING;
    xrValue.pReadOnlyStringVal = pText;

    hr = SetPropertyValue(m_dpMyTextID, &xrValue);

    return hr;
}

HRESULT UserControl1::GetMyText( BSTR* pbstrText )
{
    HRESULT hr = E_FAIL;

    XRValue xrValue;

    hr = GetPropertyValue(m_dpMyTextID, &xrValue);

    if (SUCCEEDED(hr))
    {
        *pbstrText = xrValue.bstrStringVal;
    }

    return hr;
}

I didn't change anything in the generated MainPage.h and MainPage.cpp code.

Compilation is successful, and execution is also ok. The custom controls are displayed, but the texts that I put in the xaml are not displayed.

Am I doing something wrong or missing something? I couldn't find much information on this topic on the Internet. Could anyone point me in the right direction. Thanks!

Was it helpful?

Solution

Problem solved.

I added a callback to XRDependencyPropertyMetaData.pfnPropertyChangeNotification:

dpm.pfnPropertyChangeNotification = NamePropertyChanged;

According to MSDN, PFN_PROPERTY_CHANGE is a callback method that XAML for Windows Embedded invokes when the value of the associated property changes. Indeed this callback is called during initialization. In the callback, I set the TextBlock text to the new value:

void UserControl1::NamePropertyChanged(__in IXRDependencyObject* pControl, __in XRValue* pOldValue, __in XRValue* pNewValue)
{
    HRESULT hr;
    if (pControl && pOldValue && pNewValue)
    {
        UserControl1 *tempObj;
        pControl->QueryInterface(__uuidof(UserControl1), (void**)&tempObj);
        hr = tempObj->m_pItemText->SetText(pNewValue->pReadOnlyStringVal);
    }
}

So the way of binding DependencyProperty value to a control in C++ code-behind is different from C# code-behind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top