Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:20:42+00:00 2026-05-15T23:20:42+00:00

I’ve created UserControl: public partial class MyTemplate : UserControl { public MyUser User {

  • 0

I’ve created UserControl:

public partial class MyTemplate : UserControl
{
    public  MyUser User { get; set; }
}

And then have written the following code in the main window’s xaml

<Window.Resources>
    <DataTemplate x:Key="My">
      <local:MyTemplate Margin="10"/>
    </DataTemplate>
<Window.Resources>
<ListBox x:Name="MyMainList"
       ItemTemplate="{StaticResource My}">
             ItemsSource="{Binding Path=MyTimelineTweets}"
             IsSynchronizedWithCurrentItem="True"> 

Where MyTimelineTweets has type ObservableCollection and MyTemplate user control shows data from MyFavouriteClass.

The question is:

How can I initialize MyTemplate.User in the xaml or in the code behind? //If the information about User is accessible only at the window level.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:20:43+00:00Added an answer on May 15, 2026 at 11:20 pm

    Make User a dependency property and then bind it similarly to this:

    <Window x:Name="window">
        ...
        <local:MyTemplate Margin="10" User="{Binding Foo, ElementName=window}"/>
        ...
    </Window>
    

    To make User a dependency property:

    public static readonly DependencyProperty UserProperty = DependencyProperty.Register("User",
        typeof(User),
        typeof(MyTemplate));
    
    public User User
    {
        get { return GetValue(UserProperty) as User; }
        set { SetValue(UserProperty, value); }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a generic base class for a WinForm UserControl: public partial class BaseUserControl<T>
I've created a custom control with, amongst others, the following: public partial class MyButton
I did the following: public partial class Form1 : Form { public UserControl uc
I have created an UserControl to implement a simple ImageButton as following: <UserControl x:Class=MyApp.Common.Controls.ImageButton
I've created a dependency property like this: public partial class MyControl: UserControl { //...
I have created a UserControl in WPF. This user control consists of a border,
I have created a UserControl as follows: <UserControl x:Class=MySample.MyControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:local=using:MySample xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
Here are 3 scenarios: namespace NS { public partial class A: System.Web.UI.UserControl private Variable
I have created sample User Control RestrictedBox.xaml <UserControl.Resources> <Converters:EnumToVisibilityConverter x:Key=enumToVisConverter /> <Converters:EnumToVisibilityConverterReverse x:Key=enumToVisConverterReverse />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.