Question

when i write :

    <UserControl>
        <UserControl.DataContext>
            <loca:DataBindingDemo></loca:DataBindingDemo>
        </UserControl.DataContext>

        <TextBox   Text="{Binding Path=CurrentTime}" Height="30" Background="Aqua" Margin="133,141,169,140"></TextBox>

    </UserControl>

with xmlns:loca="clr-namespace:MyClock"

it is currect and there is no problem,but when i write:

 <UserControl  DataContext="{Binding Source=clr-namespace:MyClock.DataBindingDemo}">
        <!--
        <UserControl.DataContext>
            <loca:DataBindingDemo></loca:DataBindingDemo>
        </UserControl.DataContext>
        -->

        <TextBox   Text="{Binding Path=CurrentTime}" Height="30" Background="Aqua" Margin="133,141,169,140"></TextBox>

    </UserControl>

or

    <UserControl  DataContext="{Binding  Source=StaticResource, Path=clr-namespace:MyClock.DataBindingDemo}">
        <!--
        <UserControl.DataContext>
            <loca:DataBindingDemo></loca:DataBindingDemo>
        </UserControl.DataContext>
        -->

        <TextBox   Text="{Binding Path=CurrentTime}" Height="30" Background="Aqua" Margin="133,141,169,140"></TextBox>

    </UserControl>

it does not work! why?

thanks in advance.

Was it helpful?

Solution

it does not work! why?

clr-namespace: is used to create a namespace mapping to custom classes and assemblies and can't be used directly like that in markup extension, it is not a namespace prefix.

Edit: You can't use local types in your assembly directly in your markup extension like that.

First you will have to create a namespace mapping like you have done in your question like this: xmlns:local="clr-namepsace:YOUR_ASSEMBLY_NAME.NAMESPACE_CONTAINING_TYPE_YOU_WANT".

Then you can use them in markup extensions like this:

<UserControl DataContext="{Binding Source=local:MyClock.DataBindingDemo}">
//                                          ^
//                                          |
//                                   Notice namespace using here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top