|
Collin,
I've been mulling over your post. It has brought more questions to mind. (Sorry. ) Let me preface what follows with: If I ask something really obvious or stupid, please forgive me. I have been programming for quite a few years, but I am a WPF neophyte. Ditto, if I use the wrong terminology for something. I'm not a GUI programmer ( ), but I am the only PC programmer in our group, and have been forced to jump into this with both feet. I don't really mind as I like to learn new things, but the WPF / MVVM learning curve has been frustrating for me.
Collin Jasnoch wrote: If you had a MainViewModel then the TabControl could have its ItemSource bound to a collection of your viewmodels (the data contexts of the Tabs).
The Tab Control can also maintain binding of "SelectedItem". When it changes you could then run any logic you wanted to (prompt user to save etc.)
That sounds very sensible to me. Obviously (?) my ViewModels are not exactly the same. I guess I should find what is common among them and turn that into a base class (yeah, I should have done that from the outset) that they are all derived from, so that I can create an ObservableCollection of the base class objects? Since the ItemSource is bound to the ObservableCollection, this would take care of setting the DataContext for each TabItem?
Collin Jasnoch wrote: If you choose this path, rather than having the View instantiate the VM*, the MainVM does the work. Then you just need to tell the renderer how to render the object using DataTemplates.
Right now, each TabItem in my TabControl is populated with a separate (but very similar) UserControl that consists of a 3 column Grid with a ListBox in column 0, a GridSplitter in column 1, and another Grid in Column 2. The second Grid in column 2 is a 3 row Grid with the first row containing a TextBlock that is basically a header, the 2nd row containing various TextBlocks and TextBoxes for displaying/entering data, and the last row holding several Buttons. Clicking on an item in the ListBox on the left brings up the corresponding data in the fields on the right.
How would I turn this into a DataTemplate? What about the buttons?
Collin Jasnoch wrote: I prefer this method (VM before View*) as it seems cleaner to me (no code behind)
Dumb question: isn't a certain amount of code behind unavoidable? Like for Button Click and other event handlers? I have been trying to keep what I do there to a bare minimum (i.e. the event handlers simply call "Action" methods in the ViewModel class), but I have found no way to completely eliminate it.
Thanks for your help. Sorry again about the stupid questions.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Tom Delany wrote: Like for Button Click and other event handlers Most if not all of these can be handled by the VM, we use the MVVMLight framework and it does the job nicely, and once you have the pattern set it does not vary much.
Tom Delany wrote: I have been trying to keep what I do there to a bare minimum While this probably should be the goal of all of us I don't think it should be religious. I'm always pleased to move something into the VM but I'm not distressed if there is code behind.
I figure there must be a balance of ROI on time spent figuring out how to move something into the VM and getting the job done.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks. It's nice to see common sense applied when blogs, etc. that I have read on the subject sometimes almost take on religious zealotry.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Thanks Collin. Clear as mud.
Actually, I think I understand what you mean. A couple of things I feel a little fuzzy about, but I think I have enough info. to sort it out. If I run up against something I can't figure out, hopefully you won't mind if I bug you again.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
|
Collin Jasnoch wrote: Place this DataTemplate in a resource dictionary and reference the dictionary in the UserControl that has the TabControl. It will then render the "OneOfYourViewModel" object as a "TheCorrespondingView" user control.
Create a ResourceDictionary under Application.Resources or in the MainWindows under Window.Resources, or does it really matter as long as it's closer to the root than where it is used?
Collin Jasnoch wrote: reference the dictionary in the UserControl that has the TabControl.
Will you show me a XAML example of what you mean?
Collin Jasnoch wrote: Another way is to define the DataTemplate with a key and then reference it for your "ItemTemplate".
I thought I had figured this way out, but then I stumbled because I have multiple DataTemplates and I was not sure if I still created a ResourceDictionary and gave it a key, and then bound that to the "ItemTemplate" of the TabControl, or if I had to assign a key to each template and somehow bind each one to ... something ?
Sorry. I'm a moron...
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Tom Delany wrote: Create a ResourceDictionary under Application.Resources
Typically, you create an external ResourceDictionary file and then reference it in your App.Xaml (in the ResourceDictionary section). This would look something like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AppResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
|
|
|
|
|
Thanks Pete. I'm still struggling with how to make the TabControl use the templates the way he suggested above.
I created the ITabItem interface, and the ObservableCollection of ITabItems as he suggested. I bound the TabControl ItemSource to the ITabItem ObservableCollection (which is the collection of ViewModels).
I figured out how to create a DataTemplate for each ViewModel as per Collin's suggestion. I think I could figure out how to bind one specific DataTemplate to the TabControl ItemTemplate, but I have multiple DataTemplates... Obviously I can't bind the Dictionary to the ItemTemplate.
Problem is, I can't even seem to come up with the right combination of words to Google for...
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
If you take a look at my article here[^], you should be able to see how I hooked up the DataTemplate elements to the TreeView for different ViewModels. Hopefully that should help.
|
|
|
|
|
Thanks Pete. I'll take a look at it. Sorry about the stupid questions.
After rereading the MSDN documentation about DataTemplates, and your suggestion about the ResourceDictionary (and removing the x:Key elements from my data templates ) I got it to actually sort of work. The DataTemplate documentation finally keyed me in on the fact that the way Collin was suggesting I do the templates using "DataType" would cause the DataTemplate to be "magically" applied for that type. That was what I was not seeing in my mind...
Now if I can just figure out how to make the tabs look like tabs again with headers at the top... Always something with WPF...
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Tom - they aren't stupid questions, they are just questions about doing something differently. You have to remember that declarative programming is a lot different to the world of traditional coding; getting your head around it isn't easy, and a lot of very smart people have spent a lot of time figuring how to get this all to hang together. I've read articles by these smart people.
|
|
|
|
|
I've been reading articles lately until my head hurts. They just don't seem to soak in.
I feel like you and those other smart people are in a class way above me...
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
hi everyone
can anyone help me to know how can I notificate from my application that cd/dvd is inserted or usb flash is connected?????
Thanks,
|
|
|
|
|
You can use WMI to do this in WPF.
Silverlight runs in a sandboxed environment and there is no way you can do this in SL.
|
|
|
|
|
Hi,
I have a combobox and a button on a MainPage.xaml. I want to pass the selected value from combobox to the ChildPage.xaml, on a button click.
I am new to silverlight. How can i do this ?
This is how my MainPage.xaml code look like :
<UserControl x:Class="MyChart.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="529"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" Loaded="UserControl_Loaded" xmlns:my="clr-namespace:MyChart">
<Grid x:Name="LayoutRoot" Background="White" Height="332" Width="547">
<Button Content="Display Chart" Height="38" HorizontalAlignment="Left" Margin="203,173,0,0" Name="btnDisplayChart" VerticalAlignment="Top" Width="113" Click="btnDisplayChart_Click" />
<ComboBox Height="38" HorizontalAlignment="Left" Margin="157,99,0,0" Name="comboBox1" VerticalAlignment="Top" Width="198">
<ComboBoxItem Content="Column Chart" IsSelected="True" />
<ComboBoxItem Content="Bar Chart" />
<ComboBoxItem Content="Pie Chart" />
</ComboBox>
</Grid>
</UserControl>
Please help me.
Thanks & Regards,
Swapnil
|
|
|
|
|
You do this via binding. Here is a very basic and simple example[^].
This example[^] should help you out too.
|
|
|
|
|
Hi All,
I have an odd problem occurring on a WPF app but only on Win XP (in this instance SP3) when the app is closed there is an unhandledexception thrown saying "Handle is not initialized" the odd thing is the messagebox is in my HandleException method of my bootstrapper class and is placed after a couple of lines where I log these yet I'm not seeing any logs of it either.
The app behaves absolutely normally on Win7 ... anyone seen/solved this before?
Jammer
EDIT: After running WinDbg I get this at application shutdown:
(5dc.c54): CLR exception - code e0434352 (first chance)
eax=603f68a9 ebx=00000000 ecx=0012f6d4 edx=7c90e514 esi=7c90de6e edi=00000000
eip=7c90e514 esp=0012f8e0 ebp=0012f9dc iopl=0 nv up ei pl zr na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
ntdll!KiFastSystemCallRet:
7c90e514 c3 ret
-- modified 9-Jan-12 10:47am.
|
|
|
|
|
OK, Solved this ... it was a destructor on one of my classes ... will refactor ...
Jammer
<a href="http://jammer.biz/blog2/">My Blog</a> | <a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=4664921">Articles</a> | <a href="http://www.jammer.biz/Projects/dmon.php">DMon</a> | <a href="http://www.jamsoft.co.uk/samplesort.php">SampleSort</a>
|
|
|
|
|
Hello,
I want to search text in a WPF richTextBox. My problem is the text is formatted...
For a normal text I use this:
string wordToSearch = "word";
TextRange tr = new TextRange(this.rtb.Document.ContentStart,
int startPos = tr.Text.IndexOf(wordToSearch);
this.rtb.Document.ContentEnd);
if (startPos > -1)
{
int l = this.GetLinesCount(tr.Text, startPosition);
TextPointer startRtb = this.rtb.Document.ContentStart;
TextPointer start = startRtb.GetPositionAtOffset(startPos + l);
TextPointer end = start.GetPositionAtOffset(wordToSearch.Length);
this.rtb.Selection.Select(start, end);
this.rtb.Selection.ApplyPropertyValue(
TextElement.BackgroundProperty,
this.rtb.SelectionBrush);
}
public int GetLinesCount(string str, int startPosition)
{
int lines = str.Take(startPosition).Count(k =>
k.Equals((char)13) || k.Equals((char)10));
return lines + 2;
}
But, for a formatted text (other color for example), I see a difference between my "word" and my "selection" of X characters.
If somebody has an idea?!?
Thank you for help me...
Bye.
|
|
|
|
|
I have defined a hyperlink in a HierarchicalDataTemplate. When the link is clicked, I want to capture it in the ViewModel.
The view is ProjectListView and its VM is ProjectListViewModel.
In my ViewModel I have:
private ICommand _SelectedLinkCommand;
public ICommand SelectedLinkCommand
{
get
{
if (_SelectedLinkCommand == null)
_SelectedLinkCommand = new RelayCommand(SelectedLinkExecuted, SelectedLinkCanExecute);
return _SelectedLinkCommand;
}
}
and
private bool SelectedLinkCanExecute()
{
return fileSelected;
}
private void SelectedLinkExecuted()
{
}
and in the XAML I have:
<HierarchicalDataTemplate DataType="{x:Type models:NodeModel}"
ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal"
Margin="2">
<TextBlock Margin="0,0,5,0">
<Hyperlink NavigateUri="{Binding Caption}"
Foreground="#0C2DAA"
Command="{Binding SelectedLinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type }}}">
I copied thi code from somewhere, and I don't fully understand te FindAncestor part. What am I doing wrong here?
Everything makes sense in someone's mind
|
|
|
|
|
You can't use FindAncestor in MVVM to find the VM directly because its not in the VisualTree. You would have to put the type of your View and add a Path=DataContext.
|
|
|
|
|
Ok, I think I understand, although I don't know where the Path goes
Everything makes sense in someone's mind
|
|
|
|
|
In your command binding:
Command="{Binding Path=DataContext.SelectedLinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type local:YourViewType }}}">
|
|
|
|
|
That did it . Thank you!
Everything makes sense in someone's mind
|
|
|
|
|
hi everybody
can anyone help me to know how can I rotate a usercontrol horizontally while loading??
Thanks,
|
|
|
|