I created a control which is hosting two content controls. I would like Caliburn to
resolve the View/ViewModel for me but Caliburn ignores the hosted/inner ContentControls with the name SignalGenerator (see below). Any idea how to get around that problem?
<cc:HorizontalSplitterLayoutControl >
<cc:HorizontalSplitterLayoutControl.UpperContent>
<Label Content="Blockdiagram" />
</cc:HorizontalSplitterLayoutControl.UpperContent>
<cc:HorizontalSplitterLayoutControl.LowerContent>
<ContentControl x:Name="SignalGenerator"/>
</cc:HorizontalSplitterLayoutControl.LowerContent>
</cc:HorizontalSplitterLayoutControl>
I haven't tried myself, but you should be able to do something like the following:
<ContentControl x:Name="SignalGenerator" cal:View.Model="{Binding}" />
In another project I had to do it like this:
<ContentControl cal:View.Model="{Binding SignalGenerator}" />
Related
I have an app written in WPF (MVVM), which based on some conditions, will create instances of different UserControls, These UserControls are completely independent, used to display certain information. They have some custom logic inside, like timers and so on, so I can't use Templates.
Now I face the problem that I want to create a list of UserControls in the ViewModel, and bind the host UI to it. The problem is that I don't know how to bind and what to bind. In a non MVVM project, you would simply get the layout where you want to put your controls, and add them there as children. In MVVM app, I don't know how to do this. I imagine having a WrapPanel with ItemsSource, that will add all the controls and resize itself as needed, based on the UserControls.
Can someone suggest a solution?
EDIT:
My ViewModel exposes an ObservableCollection of IMyDriver right now. So that's what I thought, to break a little bit MVVM to get what I describe next:
Now, Each IMyDriver can be a different type of driver, and can implement different other interfaces. I need the UI to create specific UserControls that know how to get maximum from these Drivers, based on their capabilities. In short, the UserControls connect to the device through the driver for polling data. And each UserControl does it in a specific way.
You can do it quite simply and easily by declaring specific data type classes for the data in each UserControl and define DataTemplates that expose your UserControls in the App.xaml file:
<DataTemplate DataType="{x:Type YourViewModelsPrefix:YourViewModel">
<YourViewsPrefix:YourView />
</DataTemplate>
<DataTemplate DataType="{x:Type YourViewModelsPrefix:YourOtherViewModel">
<YourViewsPrefix:YourOtherView />
</DataTemplate>
<DataTemplate DataType="{x:Type YourViewModelsPrefix:AnotherViewModel">
<YourViewsPrefix:AnotherView />
</DataTemplate>
Now whenever the Framework comes across an instance of these view model classes, it will render the associated view/UserControl. You can display them by having a property of the type of your view model using a ContentControl like this:
<ContentControl Content="{Binding YourViewModelProperty}" />
...
public YourBaseViewModelClass YourViewModelProperty { get; set; }
Make sure that all of your view models extend this class:
public YourViewModel : YourBaseViewModelClass { }
...
public AnotherViewModel : YourBaseViewModelClass { }
Then you can swap each view model (and display each related view) like this:
YourViewModelProperty = new AnotherViewModel();
Based on what Will commented, and what Sheridan answered, I have found the solution to my problem.
So:
I don't break MVVM by leaving ViewModel types intact.
I create DataTemplates in my Window's Resources tag, and in each data template, I assign the DataTemplate to be my UserControl defined in another assembly (UICommons)
<DataTemplate x:Key="IMultiChannelMeasurementDCDataTemplate">
<uicommon:MeasurementMax8ChannelMonitoringUserControl/>
</DataTemplate>
I create a Template Selector in my application assembly, and based on the interfaces the DataTypes implement, I return the right DataTemplate, that I assign in the same Window's Resources tag
<!-- DataTemplate Selector -->
<local:DriverComponentDataTemplateSelector x:Key="templateSelector"
DefaultDCDataTemplate="{StaticResource DefaultDCDataTemplate}"
IIhcDCDataTemplate="{StaticResource IIhcDCDataTemplate}"
IMultiChannelMeasurementDCDataTemplate="{StaticResource IMultiChannelMeasurementDCDataTemplate}"
IProgrammablePowerSourceDCDataTemplate="{StaticResource IProgrammablePowerSourceDCDataTemplate}"
IEnvDCDataTemplate="{StaticResource IEnvDCDataTemplate}"/>
I create an ItemsControl in the Window, with the following XAML code, that binds itself to my ObservableCollection of items
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ItemsControl ItemTemplateSelector="{StaticResource templateSelector}" ItemsSource="{Binding DriverComponentsInfo}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" x:Name="ucWrapPanel">
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
I enjoy dynamically created UserControls based on different Drivers!
P.S. I upvoted Will's comment and Sheridan's answer, because without these, I wouldn't be able to find the solution. Thx!
They have some custom logic inside, like timers and so on, so I can't use Templates.
This does not follow. I think you may have a misconception about the capabilities of WPF.
Also, as you want to use MVVM: Binding to a list of UserControls is breaking the pattern. View-models should only ever reference other view-models (and models); they do not know anything about the UI. Bind to a collection of view-models which have associated UserControls as their views (consider using implicit DataTemplates). To bind a WrapPanel you use an ItemsControl and set its ItemsPanel accordingly.
I created WPF UserControl which enables to enter some information now I have Main entry form where I want to use tow instances of that UserControl When I add my YserControl as Resource and than try to use it as ContrntControl's Content exception is thrown informing that control is already a logical child. Can anyone provide with sollution?
You can use the x:Shared attribute so that whenever something references the resource, a new instance is created instead of it being shared.
Thus you might have something like this:
<Window.Resources>
<MyUserControl x:Key="MyControlKey" x:Shared="False" .... />
....
</Window.Resources>
http://msdn.microsoft.com/en-us/library/aa970778.aspx
http://www.wpfmentor.com/2009/01/how-to-ensure-new-instance-of-resource.html
I just found out way to not throw exception but I want to know if this is right sollution
<TaicoControl:WizardPage Title="Title1"
BackButtonVisibility="Collapsed"
CancelButtonVisibility="Collapsed"
Description="Desctiption1"
PageType="Interior">
<ContentPresenter ContentTemplate="{StaticResource PersonEntryFormTemplate}" DataContext="{Binding Person}" />
</TaicoControl:WizardPage>
<TaicoControl:WizardPage Title="Title2"
BackButtonVisibility="Collapsed"
CancelButtonVisibility="Collapsed"
Description="Description2"
NextButtonVisibility="Collapsed"
PageType="Interior">
<ContentPresenter ContentTemplate="{StaticResource PersonEntryFormTemplate}" DataContext="{Binding Person.ContactPerson}" />
</TaicoControl:WizardPage>
I´m learning the MVVM for WPF. I know how to rout events and split the Code in View, ViewModel, Model. I have in my main-XAML a TabControl and have split the tabs into different views.
My Question is, how can i pass an object from one class to another? (From the MainWindow.cs to the SubWindow.cs)
MainWindowRessources XAML:
....
<DataTemplate DataType="{x:Type vm:SubWindow}">
<vw:SubWindow />
</DataTemplate>
<vm:SubWindow x:Key="subView" />
..
MainWindow XAML:
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
...
..
<TabItem>
<ContentControl Content="{StaticResource subView}" />
</TabItem>
...
..
You should consider implementing the mediator pattern to allow your view models to communicate with each other.
See this Stackoverflow answer for more information.
Depending on the context/use, you could create a DependencyProperty on the SubWindow class, and pass the object as a parameter, ala <vm:SubWindow MyNewProperty="some-value-or-object here"/>.
Info about creating DependencyProperties: http://msdn.microsoft.com/en-us/library/ms752914.aspx
What is the object you're wanting to pass?
Hey guys, I've got what I think is an interesting question:
You all know and love the Skype chat interface: each message is enclosed in a bubble, with emoticons and link capabilities, as well as an avatar at the left.
What is the most ideal WPF component to house each message in, if I were creating a Skype-like interface?
I am using MVVM, so all my messages are stored in the ViewModel as an ObservableCollection.
I have had problems binding to a RichTextBox, and so I have investigated binding to a Listbox, where each list item is a message and each item is styled to have a Skypey border and avatar etc.
Any ideas?
The only suitable solution that I have found is using the flowdocumentreader and an ivalueconverter to convert an array of strings to a flowdocument. It actually works great once I made my own scripting language similar to bbcode.
This was the sample I learned from. http://michaelsync.net/2009/06/09/bindable-wpf-richtext-editor-with-xamlhtml-convertor
It was a little overkill for me so I ended up just making the ivalueconverter and a simple script language.
The solution i see is that you should use DataTemplate and Style. The idea is following: each text message represented by class object. Now when you bind your message inside template, you explicit tell how do you want your messages will look like.
It will better for you to create a usercontrol that will know how represent your messages.
Example that represent similar idea, but idea is the same:
<Window.Resources>
<DataTemplate DataType="{x:Type model:MessageModel}">
<ed:Callout AnchorPoint="0,1.5" Margin="10" CalloutStyle="RoundedRectangle" Content="{Binding Path=Text}" Fill="#FFF4F4F5" FontSize="14.667" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="200" />
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding Path=MsgList}" />
</Grid>
For that example you need attach Microsoft.Expression.Drawing.sll which come aside with Blend 4.
I have a very simple case that I think would benefit from using templates (but I'm not sure, which is why I'm asking). All the templating examples I've seen either assume more knowledge than I have, are too specific to be of much use to a total newb like myself, or contain lots of ancillary stuff that makes it hard to identify what's part of the template.
Here's the setup:
I have two labels side-by-side, with the first label populated with the name of a field, and the second label populated with the value of the field.
Here is the XAML I currently have in my app (many, many times):
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}">Field One:</Label>
<Label Style="{StaticResource valueStyle}" Name="field1"
Content="{Binding dataObject.field1}" />
</StackPanel>
I would like to create a template such that I could write XAML like this:
<CustomControlOrWhatever
FieldName="Field One:"
FieldValue="{Binding dataObject.field1}"/>
I have a feeling I can do this with some kind of template. One benefit of which would be that I don't need to keep specifying the styles over and over. Am I correct? How would I do this?
Thanks in advance!
UPDATE:
Still haven't found an answer to this. I chose a possible solution using Dependency Properties, and tried to ask a clarifying question here. Well, the first responder said that I don't actually need to clutter up my code behind with DP nonsense, so I changed it again--and it still doesn't work. Can anyone come up with a working solution? This seems like it should be so simple.
Just to be clear: this only needs to be one-way binding with values updated every few seconds.
What you're asking for is basically a user control.
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SomeNameSpace.SomeControlName">
<Grid x:Name="LayoutRoot">
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}" x:Name="FieldNameLbl"></Label>
<Label Style="{StaticResource valueStyle}" x:Name="ValueLbl">
</StackPanel>
</Grid>
</UserControl>
In the code behind, you'd need to expose two properties that would set the value of the controls.
public string FieldName
{
get { return FieldNameLbl.Text; }
set { FieldNameLbl.Text = value; }
}
public string FieldValue
{
get { return ValueLbl.Text; }
set { ValueLbl.Text = value; }
}
And then to call that you can put this at the top of your window/page with the rest of your declarations:
xmlns:Controls="clr-namespace:SomeNameSpace"
and then you can insert the control into your window/page like this:
<Controls:NameOfYourControl FieldName="Field One:" FieldValue="{Binding dataObject.field1}"/>
You could create a UserControl called FieldControl and define backing (automatic) properties for FieldName and FieldValue. (Normal properties would be fine, so long as you only need to bind once, which is probably the case.)
The XAML code might look like:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="foo.bar">
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}" Content="{Binding Path=FieldName, Mode=OneTime, StringFormat='{0}: '}"/>
<Label Style="{StaticResource valueStyle}" Content="{Binding Path=FieldValue, Mode=OneTime}" />
</StackPanel>
</UserControl>
Hope that helps.
What you want to do is similar to the discussion about putting images on a button with a simple way of specifying the path, like <Button MyImage="foo.jpg" />. Follow this article for the actual details.
To summarize:
One obvious way would be to create an UserControl containing your two labels and exposing the two properties. Not much templating here.
The most WPFish solution seems to be to use two Attached Properties on one of the labels (say the value), and provide a control template for it that includes the other label (the description). In the template, you bind each label text to the corresponding attached property value.