|
I actually had a desktop app in mind, but that's quite a nice tool; I might just make it web-based, since we do have an entirely unused IIS installation on the work server. Thanks!
Will Rogers never met me.
|
|
|
|
|
I'm trying to create a lookup that can be used in many places within my application, it is to display a list of activities. It is to be used in several places, each with their own DataContext, as the application dosn't have one static context used by all forms. All instances of the context are from the same Entity Model.
The first issue i had was on one form i have a databound list view containing usercontrols as the item template, this user control contained the lookup in question. The lookup needs to share the same context as the form. This was a bit of an issue as i could not find an simple way of doing this, so i created an event in the lookup that requests a context, this is handled by the form.
The lookup contains a Dependancy Property called Value, of the type Activity, the user control binds its Activity to the Value, but also shows some other information about the activity, this is where my issue is, this fails to update when the activity is changed. Ideally i would like to avoid any process that manually updates this information, and have it done through bindings, any help would be greatly appreciated.
Form - For Simplicity i've moved the required parts of the usercontrol into the form
<ListView Name="lvwTimeCharges" Margin="0,34,0,0">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<my:uscActivitiesLookup Width="200" Value="{Binding Path='Activity', Mode=TwoWay}" ContextRequested="child_ContextRequested"/>
<Label Content="{Binding Path='Activity.sActivityCode'}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
cEntities Context = new cEntities();
Staff s = null;
Guid gStaff = new Guid("99772C66-B379-4124-839B-3427AE3481C4");
Guid gRate = new Guid("987E3837-F099-481E-BC5C-544BC04361BC");
public MainWindow()
{
InitializeComponent();
s = (from staff in Context.Staff where staff.gStaffID == s select staff).First();
DataContext = s;
lvwTimeCharges.ItemsSource = (from c in s.Charges where c.Rate.gRateID == gRate select c);
}
private void child_ContextRequested(iWantzContext sender, EventArgs e)
{
sender.Context = Context;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Context.SaveChanges();
}
Lookup
<ComboBox Name="cmbActivities" SelectionChanged="ComboBox_SelectionChanged" DisplayMemberPath="sActivityDescription"/>
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Activity),typeof(uscActivitiesLookup),new PropertyMetadata(ValueChangedCallback));
private static void ValueChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if ((obj as uscActivitiesLookup).Context != null)
(obj as uscActivitiesLookup).cmbActivities.SelectedItem = e.NewValue;
}
public uscActivitiesLookup()
{
InitializeComponent();
}
public Activity Value
{
get { return (Activity)GetValue(ValueProperty); }
set {SetValue(ValueProperty, value);}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Context != null)
Value = cmbActivities.SelectedItem as Activity;
}
private IEnumerable<TimeRecordingActivity> lookupValues;
private void PopulateCombo()
{
lookupValues = from a in _context.Activities where a.bDeleted == false select a ;
cmbActivities.ItemsSource = lookupValues;
if (Value != null)
cmbActivities.SelectedItem = Value;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
ContextRequested(this, null);
}
#region iWantzContext Members
public event ContextSharing.ContextRequestedEventHandler ContextRequested;
cEntities _context = null;
public cEntities Context
{
get
{
return _context;
}
set
{
_context = value;
PopulateCombo();
}
}
#endregion
The sActivity Code fails to update, but if a savechanges is performed on the context and the form is refreshed the Activity has been successfully updated.
modified on Friday, January 21, 2011 6:47 AM
|
|
|
|
|
Any reason why you're using data binding to bind your control to the data model, but then dropping back to WinForms-style event handling to link the combo box to the containing control?
<ComboBox
DisplayMemberPath="sActivityDescription"
DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}"
SelectedItem="{Binding Value,Mode=TwoWay}"
ItemsSource="{Binding LookupValues}"
/>
(Or you could put the actual type of your control there instead of UserControl)
To make this nice and clean, change your private "lookupValues" into either a DependencyProperty or one that fires off an INotifyPropertyChanged.PropertyChanged event.
That way, the combo is always bound to the Value property, and its ItemsSource is always bound to your internal list. You never have to catch any events from it, and you don't even need to assign a name to it. That's how WPF is meant to be used... Let the bindings do the work, so you never have to worry about keeping things synchronized.
Ed Hill _5_ wrote: private void child_ContextRequested(iWantzContext sender, EventArgs e)
|
|
|
|
|
Thanks for your reply, code project seemed to go down for a while yesterday, so was only able to read throug it now. I will give this a try and let you know how it works, or more likly if i have any questions.
Ian Shlasko wrote: Any reason why you're using data binding to bind your control to the data model, but then dropping back to WinForms-style event handling to link the combo box to the containing control?
In answer to your above question, this is my first project that i have used both entity framework and WPF so i am learning as i go, and unfortunatly when i'm not sure how to do things i fall back on the old win forms way of working. I'll get there in the end its just a but of a learning process for me.
|
|
|
|
|
Ed Hill _5_ wrote: In answer to your above question, this is my first project that i have used both entity framework and WPF so i am learning as i go, and unfortunatly when i'm not sure how to do things i fall back on the old win forms way of working. I'll get there in the end its just a but of a learning process for me.
No problem... Sorry if that came off as hostile... Wasn't sure if you had an actual reason for doing it that way - Some situation that data binding couldn't handle. But if you're just new to WPF... Well, we were all new at some point. Took me a while to get the hang of it, but now WinForms seems almost backwards by comparison.
Generally, the goal in WPF is to completely separate the GUI from the code-behind... So the GUI knows nothing about the code and the code knows nothing about the GUI. Only the model is shared between them. The only time you should even have to assign a name to a WPF control and refer to it in the code, is for those odd situations where data binding isn't good enough.
If the code needs to make a change to the GUI, it should instead be doing something to the model, which the GUI would detect and react to. It sounds unnecessary, but it's actually pretty powerful.
For example, a button on the GUI could have its IsEnabled property bound to a boolean property on the model. The property changes and the button is now ready to be used. Sometime later, you could decide to overhaul the GUI and make it flashy and attractive, so instead of just having a button enable itself, you could have the infamous Clippy fly onto the screen and pop up a little speech balloon saying "All set! Click here!", and simultaneously have a custom animated button-type thing light up with blinking text and... well, you get the idea... All of that could be triggered just by setting that single boolean property on the model, and you could do all this without making a single change to the model or the business logic.
So in short... I think the Name property is the key... If you find yourself referencing a GUI control by name in the code, you need to take a step back and ask yourself if there's a better way.
|
|
|
|
|
Ok looking back through my origional question it looks like i have said DataContext when i should have been saying ObjectContext. I believe the DataContext is being set through databinding as it should be, its the object context that I was resorting to the old windows way of doing things to pass about. If it helps i have uploaded a copy of a project i am using to test this and a screen shot of the entity model.
Object Context Screen Shot
Project Zipped
|
|
|
|
|
Ok think i have solved the issue i was having, wit the lookupnot updating the control it was on, the cause was only scalars automatically trigger a change notification, the solution was adding the following to a partial class.
public partial class Charge
{
public Charge()
{
this.ActivityReference.AssociationChanged += new System.ComponentModel.CollectionChangeEventHandler(ActivityReference_AssociationChanged);
}
void ActivityReference_AssociationChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove))
OnPropertyChanging("Activity");
else
OnPropertyChanged("Activity");
}
}
If any one is able to share a best practice way to make the lookup use the same ObjectContext as the Window is using that would be most helpful. I think the line below outlines the situation i am in, and the aim is for the cEntities Context = new cEntities(); from the window to be used in the lookup when popluating the ComboBox.ItemsSource .
Window>Databound List View>Item Template>Lookup User Control
|
|
|
|
|
Hi,
I'm new to LINQ concept the requirement what i have is, I have a string column with data as below
OR0
OR1
OR2
.
.
OR9
.
.
OR21
ORD
Now i want to order it as above, but when i do order by i get data as below as it is ordering as string
OR0
OR1
OR10
OR11
OR12
.
.
OR19
OR20
OR21
OR3
.
.
OR9
ORD
Which is wrong as per my requirement.
I tried ordering as Order By Len(Col),Col and the o/p what i received is
OR0
.
.
OR9
ORD
OR10
.
.
OR21
Can any one help me with giving me a proper Order By statement for LINQ statement?
Regards,
Kishan
|
|
|
|
|
What you're looking for is known as a "Natural Sort," and there are plenty of implementations online. Unfortunately, as far as I'm aware, LINQ does not have this functionality built-in, so you have to code it yourself.
If you can translate a bit of C# code, this site[^] has a LINQ-based solution.
|
|
|
|
|
Thank you for your kind reply.
Can i know How to get the count of Alplabets in a Alphaneumeric column using LINQ
like
OR0 - 2
OR1 - 2
.
.
OR10 - 2
.
.
ORD - 3
If i can get this, i can achieve my requirement
Thanks in advance.
|
|
|
|
|
That won't be enough for a natural sort, unless you can guarantee that your input will always be letters (possibly) followed by numbers... What if one of your items was "OR10A"? Three letters, but should be before "OR2". What happens if you have something more complicated, with multiple sets of numbers in between letters?
But... If you're absolutely sure that your input will always follow this pattern, then you could take that shortcut by writing a quick function to count the number of letters... Something like this (Sorry if my syntax isn't perfect):
Public Function NumberOfLetters(s as String) as Integer
For idx As Integer = 0 To s.Length - 1
If Not Char.IsLetter(s(idx)) Then Return idx - 1
Next
Return s.Length
End Function
|
|
|
|
|
Hi Ian,
Very mcuh thanks for your kind reply.
Actually i can sort the way how i want with a Oracle condition as below
"order by to_number(decode(COL,'ORD','1000',substr(COL,3)));"
here i'm sure that My COL will be have only the values as OR1,OR2... OR21,ORD
I just want to Implement this in LINQ as below
"ORDER BY TONUMBER(Key1 = 'ORD' ? '1000' : p.ProductName.Substring(3))"
Can you please help me to correct the above syntax as i'm sure its a wrong one.
Regards,
Kishan
|
|
|
|
|
You really need to get away from this fascination with LINQ... It's not the solution to all of your problems. In this case, you can't use the LINQ query syntax... It only works with simple properties.
But once you get the data into a collection, you can sort it via:
.OrderBy(Function(key) key == "ORD" ? 1000 : int.Parse(key.SubString(3))
|
|
|
|
|
|
Don't post here *and* in the Questions/Answer section.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hi,
I want to restrict users that are logged on a computer from a guest account from running my .NET C# application. What is the recommended way to do this?
Best Regards
Olof
|
|
|
|
|
You've already asked this in C# forum. Please, do not post the same question in several forums.
|
|
|
|
|
sorry erik, i forgot to remove this post
modified on Tuesday, January 18, 2011 5:07 AM
|
|
|
|
|
Please go through this link:
This[^]
|
|
|
|
|
Hi,
I would like to get a few expert opinions on the question of when to define a custom exception.
According to some books I have read, the exceptions built into .NET should generally be reserved for .NET's own use and developers should create custom exceptions for classes they create. This seems to me to be unnecessary - and even undesirable - at times. For example, if I had a class with a function that had a single integer parameter that should always be in the range 0-9 and it receives 42, I would have thought the best exception to throw would be the predefined System.ArgumentOutOfRangeException; it seems to fit the bill perfectly.
If you were creating a whole code library, I can see the value of defining a custom set of exceptions for the library, but for individual lumps of code, it seems like overkill.
I would be very interested to hear other opinions on this subject or get links to good articles on this subject.
Best wishes, Patrick
|
|
|
|
|
Hmmm. I've never read anything to suggest that you shouldn't be able to use the standard exceptions. It's insane for somebody to suggest otherwise because they are effectively telling you that your code cannot act like the underlying framework. What do I mean by this?
Well, suppose that you have written a framework of your own that sits on top of the .NET framework (an MVVM framework for instance), and it gains popularity with other developers. Now, this framework checks parameters to ensure they aren't null - it's only natural for the end developer to expect that a null parameter would be raised via an ArgumentNullException - after all, he's making other calls that are throwing this exception.
Suppose now, that you've used some GPL in your library, and in this code, it also checks for null arguments and in certain places, your code is such a thin wrapper that you just pass the arguments that you've got across. All of a sudden, you've got two different types of custom ArgumentNullException to handle, and that's just plain stupid.
If you have an exception type that isn't catered for by the underlying framework then, by all means, create your own exception for it - I do this quite a lot (for instance, I have an UnexpectedStringFormatException exception that I sometimes raise when I have a regular expression test that fails). If it's something as simple as an argument out of range, then use the ArgumentOutOfRangeException because that's what it's there for.
|
|
|
|
|
The authors that you're referring to have heard about the rule-of-thumb that you should inherit your own exceptions, preferably not inheriting from ApplicationException . The reasoning is that it helps in differentiating between the exceptions that the framework throws and the exceptions that you app throws.
Not considering the fact that most shops create their own framework on top of .NET, and have multiple apps on that framework. According to their rule-of-thumb, the apps shouldn't re-use the exceptions from their own framework.
..now, if programming could be reduced to a few rule-of-thumbs, we'd all be using Visual Basic. By all means, throw an OutOfRangeException if that's what's occuring - the goal of an exception is handling an exceptional situation, and if OutOfRange describes that best, then that is the one that should be applied.
I are Troll
|
|
|
|
|
I like to create my own exceptions, except (no pun intended) for the most basic validations, because the situation you mentioned is a bit rare in business applications. The custom exception often adds semantic information. In a similar situation, NegativeDepositAmmountException IMHO could be much more meaningful than ArgumentOutOfRangeException. If your number has a strict domain, often is a class or some entity that needs special treatment.
Another similar case is where you can't receive a Customer with an empty or null Customer.Address field. What would you prefer to receive? A NullReferenceException or a EmptyAdressException?
It means a lot more manual work, but in the long run it pays when you deploy your code to your customers.
|
|
|
|
|
If you have NegativeDepositAmmountException inherit ArgumentOutOfRangeException you can have the best of both worlds. You can encode as much semantic information as you need, while simultaneously making it easy for a consumer to handle common error cases without having to learn all the details of your implementation up front.
3x12=36
2x12=24
1x12=12
0x12=18
|
|
|
|
|
It's a very nice idea, but not always practical, and could lead to some inconsistencies. I learned the hard way to take a lot of care when deriving a class, and follow more rigidly LSP (Liskov Substitution Principle).
In the same example, EmptyAdressException cannot be derived from NullReferenceException, in the same way a Square cannot be derived from a Rectangle.
|
|
|
|
|