Change WPF Source in Runtime - wpf

I would like to change the source attribute in runtime from the MainWindow.xaml.cs file. My current code is displayed under here.
<controls:StartVideoCallButton Name="accountString" Source="sip:stj#starck.onmicrosoft.com" Height="75" Width="314" Background="#FFC7C7C7"></controls:StartVideoCallButton>

This works for me:
accountString.Source = "sip:new.sip#company.com";
Or you could bind the source property using the MVVM pattern to separate your UI from your business logic - this is the preferred approach for WPF apps

Related

Load catel view using ViewModelToViewConverter in xmlnsdefinition

Currently we have a WPF userControl Library where the namespaces are mapped to an XML Namespace using the
[assembly: XmlnsDefinition]
statement.
When we try to use controls out of this library in a WPF Application that references this assembly, using a ContentPresenter
<ContentPresenter Content="{Binding CurrentContent, Converter={StaticResource ViewModelToViewConverter}}" />
we always get a
System.Windows.Markup.XamlParseException("Type reference cannot find type named '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}XYZView")
What are we doing wrong - or is catel not designed to show controls using the ViewModelToViewConverter if they are in a XmlnsDefinition?
Okay, fixed - the error was on another place in the source... Catel definitely works also when using Xmlnsdefinitions.

Telerik RadDaigram does not contain definition for SwitchGridVisibility

I am trying to switch off the grid visibility of a RadDiagram I have.
I am creating a User Control using Telerik controls within a Silverlight Project (sharing the control with a WPF Library).
It seems that RadDiagram Property is not found. (RadDiagram Commands - Found property here)
PS: I am creating and filling the shape programmatically. Therefore, I need to an approach to switch the grid off that apply to Style attribute in Resource Dictionary or a simple programmatic property set / function call.
Thanks,
The way to turn off the BackGroundGrid or BackGroundPageGrid is to use these attached properties:
<telerik:RadDiagram x:Name="diagram" Margin="30"
primitives:BackgroundGrid.IsGridVisible="False"
primitives:BackgroundPageGrid.IsGridVisible="False"
where primitives is defined like so:
xmlns:primitives="clr-namespace:Telerik.Windows.Controls.Diagrams.Primitives; assembly=Telerik.Windows.Controls.Diagrams"
If you need to bind the command for switching the BackGroundGrid On / Off , you can use this syntax:
<telerik:RadButton Command="telerik:DiagramCommands.SwitchGridVisibility"
CommandTarget="{Binding ElementName=diagram}"/>
Programmatically in code-behind here is how it is done:
Telerik.Windows.Controls.Diagrams.Primitives.BackgroundGrid.SetIsGridVisible(this.myDiagram, false);
Source: Switch Grid Visibility

How to supply a design time value for silverlight textblock that is bound to viewmodel property?

I have a TextBlock in my XAML that has its text bound to a property in my viewmodel.
<TextBlock x:Name="SomeText" Text="{Binding TheTextProperty}" />
This works fine, but at design time, there is no viewmodel so the property is unresolvable and the text is blank. This is hard to work with in the designer because it shows no visible text.
How can I specify some default text to use at design time?
Maybe you could try
<TextBlock x:Name="SomeText" Text="{Binding TheTextProperty, FallbackValue='Some other text'}" />
as documented here.
You can also use Design Time Data to provide a rich binding experience in your solution. Its a little hard to set up and get running, but here's the gist.
First, you create your DataContext in xaml. Add a new Xml document to your solution (the root is a good place) and give it an .xaml extension. Lets call this file "foo.xaml" for this example.
In this file, remove all of the XML and start creating an instance of your DataContext type. For example, if your DataContext was a string (very simple example) your file would look like the following:
<string xmlns="clr-namespace:System;assembly=mscorlib">LOL!</string>
You might have yours look like
<ViewModel xmlns="clr-namespace:MyNamespace">
<ViewModel.MyObservableCollection>
<MyModel Name="foo" />
<!-- etc -->
Set the Build Action on this file to DesignDataWithDesignTimeCreatableTypes:
Next, in your View, add the following namespaces and properties to the root of your Window/UserControl:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData Source=foo.xaml}"
Source is relative to the current document. So, if your solution looked like
Views
MyUserControl.xaml
Data
foo.xaml
you would set the Source to ../Data/foo.xaml.
There are other ways to create a DesignData context depending on whether your ViewModel can be instantiated at design time, etc.
Another option is to use the DesignerProperties.GetIsInDesignMode function to determine if the control is hosted in VS/Blend and Generate a fake DataContext in that case. Laurent Bugnion provides a number of examples of how to create and use design-time data in this post

creating a nice GUI in WPF

I need to create a desktop CAD application which essentially should have a nice modern GUI.
I am thinking of creating a WPF application so that I can have a rich user interface.
Could some one suggest me a well designed desktop application GUI framework in WPF, please?
I found some cool GUI in this video http://channel9.msdn.com/posts/Psychlist1972/Pete-at-PDC09-WPF-3d-Awesomeness-with-Tor-and-Robby/
but not sure of the controls they used in their application.
Does any one have an idea which controls did they use there?
Is there any property grid control in WPF? I tried to use the grid in Windows Forms. Customizing this grid to suit my requirement seems to be difficult. It shows all the properties of the object straight from the very base class to the most derived.
With WPF, a lot is possible. You'll find a wide variety of looks to various applications due to the fact that, unlike Windows Forms, WPF can be templated and styled much like HTML. Actual designers can easily bring a look and feel which is very difficult to accomplish in Windows Forms. Naturally, since it is so flexible, the look of highly styled applications will vary a great deal from application to application.
That said, there are some very good 3rd party controls. All the usual suspects have control libraries for WPF: Telerik, Infragistics, ComponentOne, Actipro, Devxpress just to name a few. Specifically, Actipro's Property Grid is very nice. There is also an open source one which I haven't evaluated, so can't speak to. WPF can also be "themed" by applying pre-compiled styles to controls. There are example themes found here: http://wpfthemes.codeplex.com/.
Finally, WPF's strengths are not fully realized until you learn how to separate the view which gets drawn and managed by WPF and the logical abstraction of the view, called the view model. Josh Smith has a great article about this pattern, known as Model-View-ViewModel, here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx.
I think Microsoft saw no point in including a PropertyGrid control in WPF because it is so trivial to create your own, and if they created the control it would be harder to style.
To create your own PropertyGrid, just use a <ListBox> with an <ItemsTemplate> that has a <DockPanel> containing a <TextBlock> docked to the left for the property name and a <ContentPresenter> for the value editor, then enable grouping on the Category property.
The only code you need to write is the code that reflects on the object and creates the list of properties.
Here is a rough idea of what you would use:
DataContext =
from pi in object.GetType().GetProperties()
select new PropertyGridRow
{
Name = pi.Name,
Category = (
from attrib in pi.GetCustomAttributes(false).OfType<CategoryAttribute>()
select attrib.Category
).FirstOrDefault() ?? "None",
Description = (
from attrib in pi.GetCustomAttributes(false).OfType<DescriptionAttribute>()
select attrib.Description
).FirstOrDefault(),
Editor = CreateEditor(pi),
Object = object,
};
The CreateEditor method would simply construct an appropriate editor for the property with a binding to the actual property value.
In the XAML, the <ListBox.ItemTemplate> would be something like this:
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding PropertyName}" Width="200" />
<ContentPresenter DataContext="{Binding Object}" Content="{Binding Editor}" />
</DockPanel>
</DataTemplate>
I'll let you fill in the rest of the details.

PropertyValueEditor and DependencyObject in Blend 3 - Silverlight DesignTime support

I'm working on a set of controls that has a number of DependencyProperties. The properties are themselves DependencyObjects and created during the get method of the properties. During the Get method, they are also set back to the propertybag using the SetValue() method, so they are in fact valid in Xaml and their properties can be storyboarded without having to explicitly created in the the visual tree.
These DependencyObjects has all its properties as DependencyProperties as well, for supporting DataBinding. They are as mentioned above possible to use in Storyboards.
At the same time I'm developing special designtime support for Blend 3 for these properties and have created an InlineEditorTemplate in the form of a Control. I create the template and set it for the PropertyValueEditor like this:
var vectorEditControl = new FrameworkElementFactory(typeof (VectorEditorControl));
var dataTemplate = new DataTemplate {VisualTree = vectorEditControl};
InlineEditorTemplate = dataTemplate;
In the Control I have the following:
<Grid DataContext="{Binding Value}">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=X, Mode=TwoWay}"/>
<TextBox Text="{Binding Path=Y, Mode=TwoWay}"/>
<TextBox Text="{Binding Path=Z, Mode=TwoWay}"/>
</StackPanel>
</Grid>
The editor shows up and I can edit the data. And even while debugging, I see that it actually sets the data back to the DependencyProperties on the DependencyObjects, but nothing happens to the Xaml. So the data is actually not persisted in any way in the Xaml and lost when I close the Xaml file and open it again.
Is there anything I need to do specifically for it to actually get into the Xaml? I was under the impression that this would happen automatically?
Excellent Question!
The core issue you're running into a misunderstanding as to what PropertyEditors in Blend/Cider end up databinding to.
Consider this object graph:
- MyControl
-- MyControl.MyProperty
--- FooClass
---- FooClass.BarProperty
Let's look at a scenario where we have a PropertyEditor (of any type: Inline, Dialog or Extended) to property MyControl.MyProperty.
When inside MyPropertyPropertyEditor you'd expect to get a fully settable copy of FooClass and be able to manipulate it's members.
That's a good assumption, but the wrong one.
The core issue is that Blend/Cider have elaborate data structures that represent your model at design time. There's about 3-5 levels of abstraction in how Blend/Cider interact with an actual control.
Creating those levels of abstraction allows Expression Blend / Visual Studio designers to be leveraged between framewroks (Silverlight / WPF) and support advanced scenarios (like Property transactions and property chaining).
So, the value you actually get to DataBind to is just one of those levels of abstraction.
Don't believe me? In your custom PropertyEditor, register for this.DataContextChanged event and checkout the type in this.DataContext. You'll end up getting the PropertyValue class (or one of it's friends).
Every single property change you want persisted to XAML (and shown on the design surface) should go through those abstraction layers.
the question you have to ask yourself is "Where do I get one of these absteaction classes for my PropertyValue.Value property instance?".
Well, what I'd do if I were you is create a ModelItem around MyControl.MyProperty and set that as your PropertyEditor.DataContext.
We've shipped an example of using ModelFactory.CreateItem in the Silverlight Toolkit as part of the Chart DefaultInitializer: Source Code, Ning Zhang (Awesome Design Time Dev) explains about ModelItem
If you've got follow-up questions I'd consider pinging PeteBl or UnniR through the Silverlight Insiders mailing list.
Sincerely,
-- Justin
It partly solves my problem. I'm having a dialog with UnniR for a followup.
I couldn't see how I could use this together with the PropertyValueEditor, but for default values this is brilliant and something I'll implement ASAP.
Thanks.

Resources