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 7940471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:53+00:00 2026-06-03T23:23:53+00:00

I have a custom UserControl that contains a grid …I wish to set the

  • 0

I have a custom UserControl that contains a grid …I wish to set the ItemsSource property of that grid by xaml code of of a data template in a resource dictionary…
then I have used dependency property… this is my implementation…

public partial class MyControlGrid : UserControl
{

    // Dependency Property
    public static readonly DependencyProperty MyItemSourceProperty =
         DependencyProperty.Register("MyItemSource", typeof(ICollectionView),
         typeof(MyControlGrid), new FrameworkPropertyMetadata(null, OnMyItemSourcePropertyChanged));      

    IDictionary<string, string> _columns = new Dictionary<string, string>();


    private static void OnMyItemSourcePropertyChanged(DependencyObject obj, 
        DependencyPropertyChangedEventArgs args)
    {
        // When the color changes, set the icon color PlayButton
        MyControlGrid muc = (MyControlGrid)obj;
        ICollectionView value = (ICollectionView)args.NewValue;
        if (value != null)
        {
            muc.MyGridControl.ItemsSource = value;
        }
    }

    public ICollectionView MyItemSource
    {
        get
        {
            return (ICollectionView)GetValue(MyItemSourceProperty);
        }
        set
        {
            SetValue(MyItemSourceProperty, value);
            //OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));
            // Old value irrelevant.
        }
    }


    public MyControlGrid()
    {
        InitializeComponent();


    }
}

this is the user control xaml code

<UserControl x:Class="GUI.Design.Templates.MyControlGrid"
    Name="MyListControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfTkit="http://schemas.microsoft.com/wpf/2008/toolkit"
             xmlns:Templates="clr-namespace:Emule.GUI.Design.Templates">

<StackPanel>

        <WpfTkit:DataGrid   ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Templates:MyControlGrid}}, Path=MyItemSource}"
                            x:Name="MyGridControl"  

<StackPanel>

this is the binding path expression I use

<basic:MyControlGrid MyItemSource="{Binding MyDataContextVisibleCollection}"/>

this dont work and wpf output window dont show me any errors

note that, naturally, if I bind this directly in the user controls work fine

<WpfTkit:DataGrid   ItemsSource="{Binding MyDataContextVisibleCollection}"

Waths I wrong?

thanks

p.s. sorry for my english

  • 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-06-03T23:23:55+00:00Added an answer on June 3, 2026 at 11:23 pm

    this
    answer show me the way

    use of PropertyChangedCallback work fine with my code:

    public static readonly DependencyProperty MyItemSourceProperty =
                  DependencyProperty.Register("MyItemSource", typeof(IEnumerable),
                  typeof(MyControlGrid), new FrameworkPropertyMetadata(null,
                  new PropertyChangedCallback(MyControlGrid.OnItemsSourceChanged)));
    

    alternatively I have to remove comment on OnTargetPowerChanged and fire the property changed event

    set
    {
        SetValue(MyItemSourceProperty, value);
        //OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));
        // Old value irrelevant.
    }
    

    correct with

    public ICollectionView MyItemSource
    {
        get
        {
            return (ICollectionView)GetValue(MyItemSourceProperty);
        }
        set
        {
            SetValue(MyItemSourceProperty, value);
            OnItemsSourceChanged(this, new DependencyPropertyChangedEventArgs(MyItemSourceProperty, value, value));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom UserControl that contains just one TextBox . When I set
I have a custom UserControl that is defined by the following XAML: <Grid HorizontalAlignment=Left
I have a custom templated control(toolbar) that contains a custom usercontrol(button) The button uses
I have a Windows Form that contains a custom control container as a UserControl.
I have the following code in a custom user control that contains a DropDownList
I have a UserControl that contains an ItemsControl with a custom ItemsPanel, with a
I have a custom usercontrol with that code: public partial class AudioControl : UserControl
I have a FlowLayoutPanel that I fill with a custom UserControl , and I
I have a custom control that inherits from UserControl , and I can't get
In a Windows Form application I have a Form with a UserControl that contains

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.