WPF ListView that supports inline edit? - wpf

Is there a open source WPF control that looks like ListView and supports inline edit? More specifically, a cell in the ListView should be editable when you double click on it, and the edit result should be automatically saved unless you hit ESC afterwards.

I don't know of any WPF controls that do what you are looking for, but that is not indication that they don't exist. If you checkout the WPF Toolkit on Codeplex, you may find something and there are a lot of OSS projects for WPF controls on Codeplex in addition to the toolkit.
You can create one very easily taking advantage of the WPF content control model. Here is an example of a ListBox with a set of controls used to represent the objects bound to the list.
<ListBox x:Name="sampleListBox"
Width="500">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<GridRowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
</GridRowDefinitions>
<TextBox Grid.Row="0" Source="{Binding EditableProperty1}}"/>
<TextBox Grid.Row="1" Source="{Binding EditableProperty2}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can control the editability of the controls with Styles so that when the ListBox item is selected, the internal controls are editable but otherwise, they are not. I don't have access to my work laptop atm so I can't provide a style example but there are a lot of materials on the web that can help you. And if you have Expression Blend 4, it has a lot of functionality that can really help you when you are styling your own controls.

Related

WPF listbox OO data template

This is a basic question but I'm just coming back to WPF after a long break and can't remember how to do this. I've tried looking around but can't find exactly the answer I'm looking for.
I have a Listbox control that I want to bind to a List<TradeViewModel> collection. I want the ListBoxItems to pick up that the items are of type TradeViewModel and based on that, to use a custom data template including a checkbox that is bound to TradeViewModel.IsChecked and for the text of the row to be TradeViewModel.TradeId.
I have created the ViewModel class with exposed dependency properties and INotifyPropertyChanged but it's how to hook up the XAML and data templates that I can't quite remember how to do.
Can someone please help me with a quick example?
Thanks!
<ListBox ItemsSource="{Binding YourList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid> <!-- Or whatever -->
<CheckBox IsChecked="{Binding IsChecked}"/>
<!-- Other UI Elements here -->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Sample data and implicit datatyped datatemplates

Several Wpf controls have no visual representation unless they have some data (ItemsControl for example). So to view their layout in the Visual Studio designer you need to use the DesignData XAML extension to populate them with something that can be rendered. A common usage of these controls is to define several implicitly typed datatemplates so that the control can display a variety of types.
However, it seems to me that the designer has little or no support whatsoever for this most simple of layouts making it all but useless to us, but I'm hoping I'm wrong and that I've just overlooked something. Here is how it seems to me (xmlns namespaces omitted for brevity).
<UserControl
d:DataContext="{d:DesignData Source=/CustomersDesignData.xaml}">
<Grid>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock></TextBlock>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
The above works in that the designer correctly displays the sample data enabling one to verify the layout. But there is no implicit datatyping making this approach useless for displaying multiple types.
<UserControl
d:DataContext="{d:DesignData Source=/CustomersDesignData.xaml}">
<Grid>
<ItemsControl>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Customer}">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock></TextBlock>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>
</UserControl>
The above works fine at runtime and additional implicitly datatyped tempates can be added enabling the control to display multiple types. The gorilla sized fly in the ointment though is that the designer dumbly displays the raw ToString() output, making this approach useless for developing the UI.
Furthermore it seems to make no difference whatsoever where you define the template, put it in UserControl or App resources and the result is the same, nada. As far as I know there is no way to support any form of conditional compilation in XAML and the type's constructor is not run by the designer so even a code behind solution seems unlikely.
We are at the start of a project porting a sizable data application that is going to require us to design about 200 UI's, and many of these UI's are buried deep in the application. The lack of any means of being able to develop these UI's with design data displayed is already hurting our productivity and is likely to get much worse.
Has anyone else encountered a similar issue and what solutions or workarounds exist?
The solution to this problem, in my experience and in my opinion, is to set the IDE to "Open XAML files in XAML view".
In other words, don't use the designer.
If you 'need' to use the designer then open Blend, draw the designs, and then integrate them into your application. You can see each View individually. You just need to view the consolidated layout within the context of a running application. The most productive WPF and Silverlight developers I've worked with use this approach.

I want to reuse a library of WPF UserControls - How can I set the styles?

I have created a WPF desktop application using the MVVM pattern. As part of this effort, I have created a namespace with many dialog windows, dialogViewModels and dialogViews. The dialog windows typically display one of the dialogViews, depending on the assigned dialogViewModel.
I now want to convert the Views and ViewModels in this namespace to a separate library, so that I may reuse the dialogs in other applications. However, I have two questions:
How can I set the styles on my usercontrols so that, when the library is used in a different application, it uses the styles from that application.
Will I able to override datatemplate assignments made in the library controls when reusing the library?
Some code to illustrate my point:
<Window x:Class="FeehandlerMain.Dialogs.OKDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=DialogTitle}"
Height="200" Width="400" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStartupLocation="CenterOwner" SizeToContent="Height">
<Window.Resources>
<DataTemplate DataType="{x:Type dialogs:MessageBoxDialogViewModel}">
<dialogs:MessageBoxDialogView />
</DataTemplate>
</Window.Resources>
<Border Style="{StaticResource StandardBorderStyle}" >
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding}"
Grid.Column="0" Grid.Row="0" />
<StackPanel Orientation="Horizontal"
Grid.Column="0" Grid.Row="1">
<Button Content="OK" IsDefault="True" IsCancel="True"
Style="{StaticResource StandardButtonStyle}"/>
</StackPanel>
</Grid>
</Border>
</Window>
Question 1 is regarding the use of StandardBorderStyle and StandardButtonStyle. These styles are currently defined in my App.xaml file under <Application.Resources>. If I put the dialogs in a library, and I reference that library from a new application, how can I get the dialog to use StandardBorderStyle and StandardButtonStyle from the new application, so that each application can define it's own visual style?
Question 2 is regarding the DataTemplates. These templates are used to select the appropriate view for the Dialog (inserted as a ContentControl element in the example above), based on the type of ViewModel assigned as the Dialog's DataContext. Will I be able to override the above DataTemplate when reusing the library, in a scenario where I want to use a different View than MessageBoxDialogView for MessageBoxDialogViewModel?
Oh and I know it's two questions, but you will still just get reputation for one answer, sorry! ;-)
For the styles, the best way I can think of is to define a DependencyProperty for each of them in your own UserControl. So you'll have, for example, a DependencyProperty called BorderStyle which you set the default to the style you define internaly in your control library, while still allowing the client code using your control to override this style the way they want.
For the DataTemplate, you wouldn't really have to do anything, since the user of your library can define a another DataTemplate (for example for MessageBoxDialogViewModel) in his code, and that would take precedence and be used instead of the default one.
Hope this helps :)

Is there a way to touch-enable scrolling in a WPF ScrollViewer?

I'm trying to create a form in a WPF application that will allow the user to use iPhone-like gestures to scroll through the available fields. So, I've put all my form controls inside a StackPanel inside a ScrollViewer, and the scrollbar shows up as expected when there are too many elements to be shown on the screen.
However, when I try to test this on my touch-enabled device, a panning gesture (placing a finger down on the surface and dragging it upward) does not move the viewable area down as I would expect.
When I simply put a number of elements inside a ListView, the touch gestures work just fine. Is there any way to enable the same kind of behavior in a ScrollViewer?
My window is structured like this:
<Window x:Class="TestTouchScrolling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<ScrollViewer Name="viewer" VerticalScrollBarVisibility="Auto">
<StackPanel Name="panel">
<StackPanel Orientation="Horizontal">
<Label>Label 1:</Label>
<TextBox Name="TextBox1"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Label 2:</Label>
<TextBox Name="TextBox2"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Label 3:</Label>
<TextBox Name="TextBox3"></TextBox>
</StackPanel>
<!-- Lots more like these -->
</StackPanel>
</ScrollViewer>
</Grid>
You should use the attached properties:
ScrollViewer.PanningMode
ScrollViewer.PanningDeceleration
ScrollViewer.PanningRatio
The PanningMode defaults to None in the ScrollViewer default style, but setting it to another value will enable touch scrolling. I'm currently investigating using this feature in my app and am looking for a good deceleration and ratio value... I'll probably just have to test them out to find something that works well.
If you are using .NET 4.0, there is a cool thing recently released by Microsoft team!! They ported all those nice Surface controls to Win7. SurfaceScrollViewer is really cool like iphone one. Install this toolkit and start a SurfaceWin7Touch project from VS2010 project template
http://www.microsoft.com/en-us/download/details.aspx?id=26716

Use a user control as a DataTemplate within a WPF application

I am trying to create a user control within a WPF application that will serve as a DataTemplate for a ListBoxItem. The user control a grid with 4 TextBlocks. This control also contains other shapes and images more for visual aid than anything so I am omitting them from the code in this question for clarity.
When I drop the user control on my mainwindow.xaml I can see the control and the properly bound fields point to the first record in the datasource. What I want to do is have this control render repeatedly within either a listbox or a wrap panel for each record within the database.
Can anyone provide me with a pointer or sample of how to have a user control render as a DataTemplate within the ListBox control/other panel.
Up to this point I have tried the following with no avail:
Thanks in advance for any tips.
<!--within Window.Resource -->
<DataTemplate x:Key="myActivity">
<local:ucActivityItm /> <!--usercontrol -->
</DataTemplate>
<!-- Listbox within the window -->
<ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource myActivity}" VerticalAlignment="Stretch">
<ListBoxItem>
<!-- control also added for testing to ensure it rendered out-->
<local:ucActivityItm />
</ListBoxItem>
</ListBox>
That DataTemplate isn't actually being assigned to your ListBox. There's three ways:
1: Replace the template in the Resources section with
<ListBox.ItemTemplate>
<DataTemplate>
<local:ucActivityItm />
</DataTemplate>
</ListBox.ItemTemplate>
in the ListBox.
2: Somewhat related:
<ListBox ... ItemTemplate="{StaticResource myActivity}">
3: Set the DataType parameter of the DataTemplate above to whatever the content of your ListBox is.
<DataTemplate x:Key="myActivity" DataType="{x:Type ...}">
I usually just do the first, but any should work.

Resources