简体   繁体   中英

DataTemplate and ContentControl when user class as DataContext

What I have: User class

public class MyButton
    {
        public String ButtonProperty { get; set; }
        public String LabelProperty { get; set; }

        public MyButton()
        {
            ButtonProperty = "MyButtonText!";
            LabelProperty = "LabelText!";
        }
    }

DataTemplate defined in window resources

<Window.Resources>
        <DataTemplate DataType="{x:Type local:MyButton}">
               <Border Width="100" Height="100" BorderThickness="2" BorderBrush="Aquamarine">
                    <StackPanel >
                        <Button>
                            <TextBlock Text="{Binding ButtonProperty}"></TextBlock>
                        </Button>
                        <Label Content="{Binding LabelProperty}"></Label>
                   </StackPanel>
            </Border>
        </DataTemplate>
</Window.Resources>

I want to DataTemplate will draw instead of instance of MyButton class

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication7" 
    Title="MainWindow" Height="500" Width="800">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MyButton}">
            <Border Width="100" Height="100" BorderThickness="2" BorderBrush="Aquamarine">
                <StackPanel >
                    <Button>
                    <TextBlock Text="{Binding ButtonProperty}">

                    </TextBlock>
                    </Button>
                    <Label Content="{Binding LabelProperty}">
                    </Label>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>

   <!-- Create instance of MyButton in XAML-->
   <local:MyButton></local:MyButton> 


</Window>

It works fine, but it is not what I want at the end. What if instance of MyButton will DataContext for Window?

 public MainWindow()
        {
            //Set instance of MyButton as DataContext
            DataContext = new MyButton();
            InitializeComponent();
        }  

I thought I must write that in XAML-side

<ContentControl DataContext="{Binding}">
   <!--MyButton XAML code from DataTemplate here -->  

</ContentControl>


instead of

<local:MyButton></local:MyButton>

but it doesn't work at all. what I am doing wrong?

You should try to bind to the Content property of your ContentControl instead of the DataContext property :

<ContentControl Content={Binding } />

Besides, the DataContext of the ContentControl is already the MyButton.

I'm not really sure what are you trying to achieve there. IF you simply want to extend the functionality of the default Button you could define attached properties

Why would you want the DataContext of your Window to be the Button? Maybe the other way around? Not sure I understood that part correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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