Is there a way to databind to a list of doubles for editing in WPF?
I have a list of doubles which I need to display. The user can remove elements, add elements, or change the elements. Right now I am making text boxes and buttons in a for loop. Is there a quickier way to do th开发者_JS百科is with databinding?
WPF VS2010 2010 C#
Additionally to what Brady said you probably want to wrap your doubles in a class, doubles are primitive types and hence if you have a collection of them they are not guaranteed to be unique so i'd bind to a ObservableCollection<DoubleWrapper>
with DoubleWrapper
only containing one property Value
of type double
.
You can use an ItemsControl or a control that inherits from it. All you need is a DataTemplate.
<ItemsControl ItemsSource="{Binding Path=MyDoublesList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Command="{Binding MyButtonCommand}" />
<TextBox Text="{Binding Path=Description}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
There is a good overview on MSDN. http://msdn.microsoft.com/en-us/library/ms742521.aspx
精彩评论