Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF custom control DepencyProperty

WPF custom control DepencyProperty

Scheduled Pinned Locked Moved WPF
questioncsharpwpf
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wpfcomputer
    wrote on last edited by
    #1

    Hello. I create a UseControl and i'd like to add an collection DependencyProperty. ...... private static DependencyPropertyKey ItemsPropertyKey = DependencyProperty.RegisterReadOnly("MyItems", typeof(List<TextBlock>), typeof(AutoCompleteTextBox), new FrameworkPropertyMetadata(new List<TextBlock>(), MyListChanged) ); static void MyListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //my code } public List<TextBlock> MyItems { get { return (List<TextBlock>)GetValue(FileNameProperty); } set { SetValue(FileNameProperty, value); } } ......... How can I do the same with a collection of string and not of TextBlock? thanks

    A 1 Reply Last reply
    0
    • W wpfcomputer

      Hello. I create a UseControl and i'd like to add an collection DependencyProperty. ...... private static DependencyPropertyKey ItemsPropertyKey = DependencyProperty.RegisterReadOnly("MyItems", typeof(List<TextBlock>), typeof(AutoCompleteTextBox), new FrameworkPropertyMetadata(new List<TextBlock>(), MyListChanged) ); static void MyListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //my code } public List<TextBlock> MyItems { get { return (List<TextBlock>)GetValue(FileNameProperty); } set { SetValue(FileNameProperty, value); } } ......... How can I do the same with a collection of string and not of TextBlock? thanks

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      What is the problem with List ? AFAIK, You can create a DependencyProperty of any type. Just change the Type of List to List

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      Visit My Website-->**

      www.abhisheksur.com

      W 1 Reply Last reply
      0
      • A Abhishek Sur

        What is the problem with List ? AFAIK, You can create a DependencyProperty of any type. Just change the Type of List to List

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        Visit My Website-->**

        www.abhisheksur.com

        W Offline
        W Offline
        wpfcomputer
        wrote on last edited by
        #3

        The problem is that when I set the dependencyProperty on XAML I do something like: <MyControlsNameSpace:MyControl.MyItems> <TextBlock Text="First Item" /> <TextBlock Text="Second Item" /> </MyControlsNameSpace:MyControl.MyItems> But if I have a List of string I don't know How to set this property Thanks

        A 1 Reply Last reply
        0
        • W wpfcomputer

          The problem is that when I set the dependencyProperty on XAML I do something like: <MyControlsNameSpace:MyControl.MyItems> <TextBlock Text="First Item" /> <TextBlock Text="Second Item" /> </MyControlsNameSpace:MyControl.MyItems> But if I have a List of string I don't know How to set this property Thanks

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          Well my friend, why don't you use ItemsControl in this regard. You can go for ListBox.

          <ListBox ItemsSource="{Binding}" DataContext="{StaticResource MyItems}">
          <ListBox.ItemTemplate>
          <DataTemplate>
          <TextBlock Text="{Binding MyProperty}" />
          </DataTemplate>
          </ListBox.ItemTemplate>
          </ListBox>

          If you are using List of STRING you dont need to specify property name. Use Text="{Binding}" which will eventually call ToString for the string object and get the value of it.

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          Visit My Website-->**

          www.abhisheksur.com

          W 1 Reply Last reply
          0
          • A Abhishek Sur

            Well my friend, why don't you use ItemsControl in this regard. You can go for ListBox.

            <ListBox ItemsSource="{Binding}" DataContext="{StaticResource MyItems}">
            <ListBox.ItemTemplate>
            <DataTemplate>
            <TextBlock Text="{Binding MyProperty}" />
            </DataTemplate>
            </ListBox.ItemTemplate>
            </ListBox>

            If you are using List of STRING you dont need to specify property name. Use Text="{Binding}" which will eventually call ToString for the string object and get the value of it.

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            Visit My Website-->**

            www.abhisheksur.com

            W Offline
            W Offline
            wpfcomputer
            wrote on last edited by
            #5

            Hello I'm not so good in wpf. If I understand well you say that I have to change my property type to ItemsControl intead of String and add a ListBox with that XAML on my custom control. Is it correct?

            A 1 Reply Last reply
            0
            • W wpfcomputer

              Hello I'm not so good in wpf. If I understand well you say that I have to change my property type to ItemsControl intead of String and add a ListBox with that XAML on my custom control. Is it correct?

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              Yes you are in right path.

              Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


              Visit My Website-->**

              www.abhisheksur.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups