WPF custom control DepencyProperty
-
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 -
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? thanksWhat 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-->**
-
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-->**
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 -
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 ThanksWell 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 callToString
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-->**
-
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 callToString
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-->**
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?
-
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?
Yes you are in right path.
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->**