|
The reason I suggested an ObservableDictionary is that it, by default, solves the problem where you can't have an entry with the same key name. One thing I would say - move your validation out of the binding into a ViewModel, it's a lot easier to test and you can make your rules a lot more flexible, especially as you would have full programattic access to your ObservableCollection.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete,
Thanks for the suggestion of using a ViewModel. I am a big fan of using ViewModel and have made extensive use of this pattern in our project (where it fits). I found your article "Action based ViewModel and Model validation." and Josh Smith's article "Using a ViewModel to Provide Meaningful Validation Error Messages" on this approach (I assume that is what you meant), and have now modified the project accordingly.
So the original class was ATUser and I defined a new UserViewModel class which wraps ATUser. I have been able to achieve the validation (and it works nicely), however I am still not sure I am following good programming practise (really my original question), because I found it essential to pass a reference to the ObservableCollection (container collection), into each UserViewModel member, so the member could find out if the user name had already been used.
So a snippet of UserViewModel is
Public Class UserViewModel
Implements INotifyPropertyChanged
Implements IDataErrorInfo
Private _user As ATUser
Private _containerCollection As AUSObservableCollection(Of UserViewModel)
Public Sub New(ByRef User As ATUser, _
ByRef ContainerCollection As AUSObservableCollection(Of UserViewModel))
_user = User
_containerCollection = ContainerCollection
_username = _user.Username
End Sub
'lines removed
Default Public ReadOnly Property Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item
Get
Select Case propertyName
Case "Username"
'logic to determine if Username is valid, which involves searching through _containerCollection
End Select
End Get
End Property
End Class
So I believe I have achieved the goals of using the ViewModel, which include improved control over Data Validation. However I'm not sure I addressed my original concern - which is holding a reference to the containing collection within the member object itself.... Could you comment on whether I have missed the point / whether there is a more acceptable way to solve this / whether the reference to the containing collection is OK? Just to restate, in order for a UserViewModel to validate the Username property, it needs to access the containing collection, in order to see if the name has already been used.
From the Data Validation point of view, I could have achieved the same by implementing IDataErrorInfo on the ATUser class, and simply adding a Property to hold the reference to the containing collection (which would take much less time and be easier to maintain, but would loose the other benefits of ViewModel). I have about 9 similar classes in this project, so I would like to get this right before moving on.
Thanks again for your help, and apologies if I am asking a basic question.
Tim
|
|
|
|
|
Another alternative I have discovered is to use a shared member property of a Helper object to hold the collection that needs to be searched.
I can define this as follows:
Public Class ATBusinessInfoHelper
Private Shared _userContainer As AUSObservableCollection(Of ATUser)
Public Shared Property UserContainer() As AUSObservableCollection(Of ATUser)
Get
If _userContainer Is Nothing Then
Throw New AUSException("UserContainer has not been defined")
Else
Return _userContainer
End If
End Get
Set(ByVal value As AUSObservableCollection(Of ATUser))
_userContainer = value
End Set
End Property
End Class
Then in the IDataErrorInfo validation logic, I can step through ATBusinessInfoHelper.UserContainer to see if there is a conflict with an existing name. Would this be a valid approach to take?
Thanks for any advice,
Tim
|
|
|
|
|
I am new to wpf.
I have created a reflective textblock usercontrol .
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="LogoCreator.ReflectiveTextBlock"
Height="50" Width="189" mc:Ignorable="d">
<UserControl.Resources>
<VisualBrush x:Key="ReflectionBrush" Visual="{Binding ElementName=textBlock}"/>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="textBlock" Grid.Row="0" Margin="0" FontSize="18.667" Text="Sample1" TextWrapping="Wrap" HorizontalAlignment="Left" Background="White"/>
<Rectangle x:Name="Reflection" Grid.Row="1" Margin="0,1,0,-32" RenderTransformOrigin="0.5,0.5" Fill="{DynamicResource ReflectionBrush}">
<Rectangle.OpacityMask>
<LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
<GradientStop Color="#7EFFFFFF" Offset="0"/>
<GradientStop Offset="0.5"/>
</LinearGradientBrush>
</Rectangle.OpacityMask>
<Rectangle.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="-1"/>
</TransformGroup>
</Rectangle.LayoutTransform>
</Rectangle>
</Grid>
</UserControl>
I have the rectangles fill property set to the textblocks visual brush.
The problem is when this control is add to the main window canvas, it shows only the text, the reflection is not shown.
Is it because the visualbrush resource is within the usercontrol ?
How do i solve it ?
|
|
|
|
|
Ive solved it by replacing the rectangle with border. And setting the borders background to visualbrush binded to textblock, without using resources.
I think the problem was that the visualbrush resource was within the usercontrol.
Thank you.
|
|
|
|
|
I am seeking help from the pros of WPF and 3D graphics to recommend books on the title mentioned that helped them. I did my search on Google but most examples assume you already know what the deal is (first time to try 3D in WPF).
Basically I have a 3D Model (plain) and a (Colored one or with texture). How to export it to something that WPF understands plus I want to use the mouse as the interacting device finally some properties the user can select and when a button is clicked the model change accordingly.
Is it also possible to convert the current camera view to an image and use it in Crystal reports?
Can I render many models at the same time (without showing it) and export the result to image and use it as a data source in crystal reports?
Can the user select the view of the camera view (via check box) before rendering the model (hidden) then use it in crystal report?
Suppose I have a cube, can I select a side from the cube and right click on it to get a menu similar to the menu when you right click on the desktop?
I am not asking for an implementation, only good references to help with the problem.
Thank you
|
|
|
|
|
I think Adam Nathan's WPF Unleashed[^] is one of the best on the market, with a good section on 3D.
[edit]Changed category to general[/edit].
|
|
|
|
|
Depending on what program your using for your 3d model, some programs will actually export the model as an xaml object. Makes life great .
|
|
|
|
|
All i want is to use MVVM pattern
I should use a form
example:
customer form which contains customer name and customer id
probably the application should use command when the save button is clicked
and add this data to the table in the database.
Can somebody send me how to do this.
I know how to do this with out the MVVM pattern
|
|
|
|
|
|
In addition to the article above, which is pretty much the MVVM standard, it seems, I would like to stress that you should take a long hard look at commands, templates and databinding before writing any code. Josh Smith's article uses commands in a different way from some others. This[^] article has IMO a nicer way of managing commands. Also, this[^] is a nice cheatsheet for WPF databinding.
|
|
|
|
|
I am looking for a small example in MVVM pattern.
I should be able to add data to the database and display data from the database
using MVVM pattern
If you have any thing like that send me or please post a link to it.
|
|
|
|
|
|
I have a simple UI I'm trying to write using the MVVM pattern:
Click a "browse" button, the OpenFileDialog should show up, let the user select a file, and then the "selected file" textbox should be filled with the selected file.
How do you do this simple thing in MVVM?
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Have a look at Onyx[^]. This has this functionality all built in for you.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Awesome, thanks Pete.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
No probs mate.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
how I can creat or use a plotter(osciloscope) in my project(wpf)?
|
|
|
|
|
Please use a better title than Wpf - we know that this forum is WPF, and we don't need you to tell us that. Use the title to give us an indication of what your problem is, other than an inability to come up with a working title.
miralireza wrote: how I can creat or use a plotter(osciloscope) in my project(wpf)?
You're probably going to need the API for your Oscilloscope. This isn't a WPF specific question - it's more about how you interface with an external system. Your use of WPF is a secondary consideration.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
HI, This is a simple one, but i cant get it to work
I have a combobox filled with color names and a small colored rectangle next to the names.
I want to bind this combobox to a text box, such that when a color is selected in combobox, only the color name shows up in the text box. I need to do this programatically, not in xaml
My combobox datatemplate is
<DataTemplate x:Key="ColorText" >
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="28" Fill="{Binding Name}" Stroke="#FF000000"/>
<TextBlock Text ="{Binding Name}" />
</StackPanel>
</DataTemplate>
My current code to bind is
Binding myBinding = new Binding("Text");
myBinding.Source = this.colorTextBox;
myBinding.Mode = BindingMode.Default;
this.comboBox.SetBinding(ComboBox.SelectedItemProperty, myBinding);
But the result i get in text box is "System.Windows.Media.Color Blue" , i need only Blue to appear in the text box.
comboBox.selectedItem object contains 2 types, one is color and other is Name as the datatemplate shows.
Please help me to modify my above binding code , such that the text box shows only the name of the color and not "System.Windows.Media.Color Blue".
These controls(combobox and textbox) are programatically generated.
|
|
|
|
|
Use a converter.
Create a class that implements IValueConverter, and in the Convert() function, change it from "System.Windows.Media.Color.Blue" to "Blue" with some simple string parsing. Then just set it as the Converter property of the binding.
Your code is a bit confusing, though... You're binding the selected item to a textbox? Does the Name property contain an actual color object, or a name of a color? Something doesn't seem quite right.
|
|
|
|
|
The name property contains only a string eg. "Blue"
The comboBox is filled with colors from Colors list.
Helper h=new Helper();
this.comboBox.ItemsSource =h.GetPropNames(typeof(Colors));
The first property is a color and second one is Name, hence the full string looks like this "System.Windows.Media.Color.Blue Blue". I want to bind the comboBox.selectedItem.Name to the textbox, so the textbox shows only "Blue"
I used value converters and it works , Thank you
|
|
|
|
|
Dear Friends,
See I want to Clear My IsolateStorage once the Browser is Closed In Silverlight.
I Have I Tried with Evry Javascript Posssible.I Dont Wnat to Clear my Isolated
Storage When Browser is Refreshed.So How Should I Detech Browser Closed
And Refres.
Thanks in Advance.
Regards,
Muneer.
Muneer
|
|
|
|
|
How can we capture the Keypress event and the pressed key, on a WPF toolkit datagrid? I need to capture the F9 key, when the user is focussed on a particular column.
Plus, can anybody suggest a good site to study the WPF toolkit datagrid? The basic CRUD operations need to be implemented. I am totally new to .NET 3.5.
Thanks Folks!!!
|
|
|
|
|
I'm having a similar problem; I need to implement a double-click on the datagrid, while using MVVM. So I don't want to have any event handling code in the code-behind. Preferably binding to a command property in the VM.
I've searched the internet all ower and still cannot find a suitable solution. Is there somehow a way (for me and sameercodes) to handle these keyboard and mouse events with behaviours or gestures AND bind to a command
Thanks. 
|
|
|
|