I am using WPF Interoperability (ElementHost) to display a XAML control in WinForms. This works perfectly for my needs as I must use WinForms for a project I am working on. The issue is that when I add images, such as icons, to the Ribbon in XAML, I can no longer add the XAML Ribbon to the WinForm. The error I keep getting is:
An error occurred trying to create an object of type 'WPF.Ribbon'. Make sure the type has a default constructor.
The images have a Build Action of "Resource". Here is an example XAML code snippet I am using for my ribbon code:
<RibbonButton SmallImageSource="/App/Images/Ribbon/cut.png" Label="Cut" KeyTip="X" />
When I remove the image source, I can compile the code fine, and the ribbon displays without any issue.
The resolution is to add absolute addresses, such as:
<RibbonButton SmallImageSource = "C:\Path To Images\App\Images\Ribbon\cut.png" Label="Cut" KeyTip="X" />
Related
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
I am working on a project right now that has to separate project files. One complies in Silverlight 3 and the other in Silverlight 4. The project creates a user control that is used in a couple other projects. Unfortunately one is in SL3 and cant be upgraded right now which is why a SL3 version of the control is needed.
So far it has worked out fine but recently I tried to add a ViewBox to the control. This causes a problem because in SL3 it is located in the toolbox and in SL4 it is in the core.
Is there any way to have it pull from the toolkit in the SL3 project and the core in the SL4 project? I looked into preprocessor definitions in xaml and found some info but I don't like how it kills the ability to load the code in the designer.
What I ended up doing is creating a border around the inner viewbox elements. Then I got rid of the viewbox in xaml. When I load the control I set the child of the containing border to null, create a viewbox in code, and set its child to the inner border. Then I set the child of the containing border to the viewbox.
<Border Name="viewBoxContainer">
<Border x:Name="innerBorder">
<TextBlock x:Name="innerText" Text="Test" />
</Border>
</Border>
Code Behind
this.viewBoxContainer.Child = null;
this.viewBoxContainer.Child = new Viewbox { Child = this.innerBorder};
No, there is no support for conditional complication within XAML. How about creating your own subclass within each project:
within the SL3 project:
using //namespace for SL3 ViewBox
namepsace MyProjectNamespace
{
public class MyViewBox : ViewBox
{
}
}
within the SL4 project:
using //namespace for SL4 ViewBox
namepsace MyProjectNamespace
{
public class MyViewBox : ViewBox
{
}
}
within your common XAML file:
<UserControl ...
xmlns:local="clr-namespace:MyProjectNamespace">
<local:MyViewBox>
.. content goes here ..
</local:MyViewBox>
</UserControl>
We had the same problem, and our solution was to make our own viewbox that was used across the board.
Bascially, we just took the source code for Viewbox provided in the SL3 toolkit, copied it and put it in our own namespace, then renamed it to something like MyViewbox.
I'm not sure this is the best solution, but it worked out well for us.
This is a bit of a WPF whodunnit! My validation temples it not appearing when expected
The Problem
Basically, I am using IDataErrorInfo on my view model to provide feedback on data entry errors. I can see this being called and working as expected, but the validation error template for the bound control is never displayed by WPF!
I have tried both using the built-in default textbox red border error template, a style error template, and a hard coded template. None of these are displayed in a data error situation.
Heres my xaml binding code:
<TextBox x:Name="txtCaseNumber"
Text="{Binding Path=CaseNumber, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
Wierdly, the error gets into the Validation.Errors collection. The follow code results in my IDataErrorInfo error message getting displayed next to the bound control. This leads me to think the problem is on the xaml side, rather the viewmodel side.
<ContentPresenter
Content="{Binding ElementName=txtCaseNumber, Path=(Validation.Errors).CurrentItem}" />
Additionally,
System.Windows.Controls.Validation.GetHasError(txtCaseNumber) returns true.
I am hosting WPF in winforms if that makes a difference. I can't find any references in my code or styles to Validation or ErrorTemplate so I'm pretty sure this isn't a styling own goal.
Cracked it.
The ErrorTemplate attached property uses the adorner layer to show error templates on data validation failures reported via IDataErrorInfo. The Adorner layer is not always available for all controls in all situations. Your control or one of its parents needs to explicitly provide one.
The default Window control template contains an AdornerDecorator which enables the adorner layer for its children. This why my other window showed my error templates just fine, because this window used the default control template
The window which didn't show my error template had used a style which provided a ControlTemplate for my window. This control template did not provide an <AdornerDecorator>, hence no adorner layer to show my error template.
The fix was as simple as wrapping my window control template with <AdornerDecorator>.
I am having a strange issue opening up my UserControl in Expression Blend when using a Silverlight Toolkit control. My UserControl uses the toolkit's ListBoxDragDropTarget as follows:
<controlsToolkit:ListBoxDragDropTarget mswindows:DragDrop.AllowDrop="True" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<ListBox ItemsSource="{Binding MyItemControls}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</controlsToolkit:ListBoxDragDropTarget>
Everything works as expected at runtime and looks fine in Visual Studio 2008. However, when I try to open my UserControl in Blend I get XamlParseException: [Line: 0 Position: 0] and I can not see anything in the design view. More specifically Blend complains:
The element "ListBoxDragDropTarget" could not be displayed because of a problem with System.Windows.Controls.ListBoxDragDropTarget: TargetType mismatch.
My silverlight application is referencing System.Windows.Controls.Toolkit from the Nov. 2009 toolkit release, and I've made sure to include these namespace declarations for the ListBoxDragDropTarget:
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
If I comment out the ListBoxDragDropTarget control wrapper and just leave the ListBox I can see everything fine in the design view without errors. Furthermore, I realized this is happening with a variety of Silverlight Toolkit controls because if I comment out ListBoxDragDropTarget and replace it with
<controlsToolkit:BusyIndicator />
the same exact error occurs in Blend. What is even weirder is that if I start a brand new silverlight application in blend I can add these toolkit elements without any kind of error, so it seems like something dumb that is happening with my project references to the toolkit assemblies.
I'm pretty sure this has something to do with loading the default styles for the toolkit controls from its generic.xaml, since the error has to do with the TargetType and Blend is probably trying to load up the default styles.
Has anyone encountered this issue before or have any ideas as to what may be my problem?
Hi we had exactly the same issue, we solved it by checking the references in the project which was having this problem. All referenced toolkit assemblies should be in the same directory on disk.
Our project reference to the System.Windows.Controls.Toolkit.dll always 'jumped' back to the orginal path which was causing our issue. We solved by editing the project file in notepad++ (or any of your favorite text-editor) and hardcode the path where it could find the assembly.
Hope this helps.
For Visual Studio (maybe Blend too) you need to add a reference to :
System.Windows.Controls.Toolkit.Internals.dll
"C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Bin\System.Windows.Controls.Toolkit.Internals.dll"
I'm using Silverlight 5 toolkit and having XamlParseException when using BusyIndicator toolkit control in Expression Blend for SL 5, all above solutions didn't helped but I found another workaround, its rather dirty but allows me to make BusyIndecator work in Expression Blend,
derive from control
public class BusyIndicatorEx : BusyIndicator
{
public BusyIndicatorEx()
{
this.DefaultStyleKey = typeof(BusyIndicatorEx);
}
}
create style for derived control (just copy style from BusyIndicator source code to themes/generic.xaml, and change target type to local:BusyIndicatorEx)
I had the same problem which has been resolved by introducing a dummy reference in code behind to the Wrap Panel.
I know it is a little C++y but I can imagine it is because we have indirect reference to wrap panel inside a template and not on the top level page so the loader don't know what to load on initialization. I really like understand the exact reason, though.
I just referenced System.Windows.Controls.Toolkit and introduce following member in code behind:
System.Windows.Controls.WrapPanel _dummy;
I need to instantiate WPF types (say, a UserControl or a Page) via reflection for a designer. The problem I'm having is that when I attempt to instantiate these using Activator.CreateInstance I get a TargetInvocationException which wraps, in the end, an exception thrown by the StaticResource markup extension.
Clarification: The types are in a different assembly that is loaded at runtime!
Apparently, Activator.CreateInstance can't instantiate types that use the {StaticResource XXX} markup extension, even when the static resource is defined in the type's definition.
So, if all you have is the following type's definition (Type + xaml file):
<Page x:Class="Hurr.Durr">
<Page.Resources>
<ControlTemplate x:Key="whatever">
<TextBlock Text="This is a stupid example."/>
</ControlTemplate>
</Page.Resources>
<ContentControl Template="{StaticResource whatever}" />
</Page>
How would you spin this up at runtime?
BTW, requiring that you use DynamicResource instead is not acceptable. Does Visual Studio require this? Yet VS is able to spin up a copy of your WPF framework element and stick it in the designer.
Here's a sample application (beta 2, sorry) that demonstrates the issue.
http://cid-f8be9de57b85cc35.skydrive.live.com/self.aspx/Public/ResourcesGoKaboom.zip
The app attempts to load a Page with a drawing in its resource dictionary. I try to load it dynamically using Application.LoadComponent and Activator.CreateInstance and they both fail.
Okay, the example above works without an issue. The issue was that I was using a StaticResource within an element's attributes that referenced a resource defined within that element.
What was throwing me was that this works within visual studio. So in the designer everything looked okey dokey but IRL it was all pear shaped. IMHO, this is a bug and I've reported it as such:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=519244
Not sure if its a failure of VS to.... fail, or if the StaticResource extension was working in VS but not IRL.
The problem appears to be with the Page you're trying to load. Your Page looks like this (omitting a bunch of xmlns etc. plumbing):
<AwesomePage Title="AwesomePage" Background="{StaticResource awesomeface}">
<AwesomePage.Resources>
<DrawingBrush x:Key="awesomeface" Stretch="Uniform">
<!-- details snipped -->
</DrawingBrush>
</AwesomePage.Resources>
</AwesomePage>
This tries to use the awesomeface resource before it is defined. StaticResource does not allow this kind of forward referencing: therefore the StaticResourceExtension.ProvideValue fails and you get the error you have run into.
To fix this, put a Border immediately inside the Page, and move the Background setter from the Page to the Border. Then put all your content inside the Border.