I want to add a behaviour to a TextBlock in silverlight 3.
I have a behaviour class in a c# file in a different project than my xaml file within my solution.
public class FooBehavior : Behavior<TextBlock>
{
...
}
How do I attach this behaviour to my TextBlock? Would be nice to do without involving c# code.
Include the following lines in the definition of your UserControl:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:myBehaviors="clr-namespace:MyNamespace.Behaviors;assembly=MyAssembly"
Then on the TextBlock have this code:
<TextBlock .....>
<i:Interaction.Behaviors>
<myBehaviors:FooBehaviour/>
</i:Interaction.Behaviors>
</TextBlock>
ChrisF has the correct answer for how to write the Xaml to add the behavior. However, if you have Blend it is even simpler.
Open your project in Blend
On the tools toolbar click the >> button
Click on Behaviors
Find your Behavior and Drag it over your TextBlock and drop it
Blend will add all the proper namespaces for you.
Related
I am creating a custom ToolBar control that I will be able to use in my WPF application.
My CustomToolBar control is based on a StackPanel control and will contain multiple CustomToolBarButton controls.
My CustomToolBarButtons are based on a vertical StackPanel and contain an Image control and a Label control.
I want to programmatically create a Click event for my CustomToolBarButtons that will fire when Image or label gets clicked. Unfortunately, neither Image nor Label controls have Click events in WPF.
It's a surprise for me because I am used to WinForms controls and vast majority of them have Click events by default. Do I have to create custom Image and Label controls and create Click events for them or is there a cleaner and simpler way of doing this?
Thanks for any help!
Well, there's not a great and simple way using standard functionality. I got around this by making my own trigger (derived from System.Windows.Interactivity.TriggerBase from the Blend SDK) so that in my projects I can do the following:
<Label>
<i:Interaction.Triggers>
<mu:MouseTrigger MouseButton="Middle" MouseAction="Click">
<i:InvokeCommandAction Command="{Binding Path=Close}" />
</mu:MouseTrigger>
</i:Interaction.Triggers>
</Label>
Effectively the MouseTrigger class will handle MouseDown and MouseUp events from the UIElement it's attached to and use that to invoke the actions associated with the trigger. The code is a bit more complex than just that, though, since I also do mouse capturing & I utilize an internal helper class so that I could add multiple triggers to the same element with only one instance of a helper class handling events & capturing the mouse for that element.
For the actual actions, I just use existing Blend or Prism actions such as InvokeCommandAction.
Here is the project if you're interested. It was too large to paste into this format. It uses some C# 6.0 features but you could easily modify it to work on an older version of C# by removing some null conditional operators. It requires you install the Blend SDK as it depends on System.Windows.Interactivity (should install with Visual Studio as long as you select that option). MouseTrigger is the publicly visible class that is the point of interaction with the functionality. MouseCatcher is the internal helper class mentioned.
I'd recommend not going down the route of custom controls, but rather of using some mechanism (this one or otherwise) to extend the existing functionality using the attached property framework that WPF and XAML bring to the table.
In WinForms you would create custom controls simply to get a nonstandard look, in WPF this is no longer necessary.
The WPF way: instead of making a Stackpanel's children implement Click behaviour, let's make a Button look like the desired Stackpanel.
Could do this setting the Content:
<Button>
<StackPanel >
<Image Source="C:\myFiles\myPic.png"/>
<Label HorizontalAlignment="Center">SomeTxt</Label>
</StackPanel>
</Button>
But this way it still looks like a Button, to overcome this we can set the Template instead:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel >
<Image Source="C:\myFiles\myPic.png"/>
<Label HorizontalAlignment="Center">SomeTxt</Label>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
Both solutions will raise a Click event clicking the image or label.
I have a working application that has been written in C#, and I now want to extend that application to allow the user to switch between viewing the application, and viewing a built in web browser (inside the same application window).
I also have a separate working web browser, that has also been written in C#.
I have just added the functionality to the original application to include 'tabbed' displays, where the original application will be displayed on the first tab, and a built in web browser on the second tab.
The 'tabbed' displays for the application have been created using XAML markup in Visual Studio. I now want to add an instance of the Web browser that has also been written in C# to the second tab that I have created in the XAML markup.
It would be something like:
<TabControl>
<TabItem Header="Browser">
<StackPanel>
<!-- Call/ instantiate the browser here -->
</StackPanel>
</TabItem>
</TabControl>
But I have no idea how I call/ create an instance of the browser from within the XAML markup...
The browser has been created using C#:
namespace Agent
{
public partial class Browser : Form
{
public Browser()
{
...
}
}
}
Can anyone explain to me how a create an instance of Browser inside the ` of the XAML markup?
Edit
Ok, so I have edited my XAML markup as recommended in the answer that's been suggested- I now have:
<Window ...
xmlns:Agent="clr-namespace:Agent"
...>
<Grid>
...
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
</Grid>
</Window>
I have also updated my Browser.cs class, so that it is now extending UserControl, rather than Form:
public partial class Browser : UserControl{
However, I am getting a compile error on the line:
<Agent:Browser x:Name="Browser" />
which says:
The name "Browser" does not exist in the namespace "clr-namespace:Agent".
But clearly Browser does exist in Agent, as shown by the code I've included here... In fact, when typing the line <Agent:Browser x:Name="Browser />, when I typed the :, Browser was one of the options that came up in the autocomplete menu...
What am I doing wrong here? Why doesn't the compiler think that Browser exists inside Agent?
The key to instantiating any object in XAML is to make sure the namespace is declared. You can provide any XML prefix and assign it to your CLR namespace (ref) and it will act like a using statement. For example:
<TabControl xmlns:agent="clr-namespace:Agent">
<TabItem Header="Browser">
<StackPanel>
<agent:Browser/>
</StackPanel>
</TabItem>
</TabControl>
NOTE: your object has to extend UIElement (or one of its children) for it to work in a XAML tree. If your control is a WinForms control you either need to find the equivalent XAML control or wrap it in a WindowsFormsHost (ref).
WPF vs. WinForms
The purpose of this section is to help recognize which platform code is by namespace, as well as some of the trade-offs. I've used both and can say from experience that they each have good points and... not so good points.
WinForms classes live in the System.Windows.Forms namespace, and are available by referencing the System.Windows.Forms.dll assembly.
WPF classes live in the System.Windows and System.Windows.Controls namespaces, and are available by referencing a set of DLLs
WinForms rendering is immediate. That means you are working against bitmaps and you are responsible for clearing and redrawing stuff yourself (usually you can just call Invalidate()). If you do heavy image bit manipulation, WinForms is easier to work with.
WPF rendering is declarative. That means more work is offloaded to your GPU and you just tell it how to draw stuff. You can also use GPU render shaders for special effects. WPF has a nicer look out of the box, but it has a reputation for making easy things difficult but impossible things possible.
WinForms is easier to learn, but has a dated look out of the box.
WPF is built around data binding, enabling the UI to update in response to property values automatically. It's also able to be completely restyled, although that is quite an undertaking.
If you are just getting started, I'd go ahead and bite the bullet to start the heavier learning curve for WPF. It will provide a basic understanding that transfers to other platforms like Windows Store apps, etc.
Firstly you need to place that tag inside of your UserControl opening tag like so:
<UserControl x:Class="View.testControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Agent="clr-namespace:Agent">
Then you can use it like this:
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
EDIT
From what you told me in the comments you will need to create a Custom Control in your WPF project. To do that you need to:
Right Click your Project;
Select Add New Item;
From Installed (which is in the left column) select WPF;
From the list in the middle column select Custom Control;
Now you can create that control in your XAML with xmlns attribute.
Here is a great example from msdn on how to create custom controls
Hi I'm trying to learn a bit of WPF and C# for a project I'm working on. I realised after working for a while that I was going to use couple of the same item and I thought it would be fitting to create my own class to make it easier to use. However now I can't reference elements defined in the XAML in the code behind like I could when just doing it in a window. The error I get is "Cannot resolve symbol 'ThaButton" in the ContactPanel.xaml.cs:
using System.Windows.Controls;
namespace WPF_OOM
{
public partial class ContactPanel : ContentControl
{
public Contact Person { get; set; }
public ContactPanel()
{
ThaButton.Content = "test";
}
}
}
My ContactPanel.xaml
<UserControl x:Class="WPF_OOM.ContactPanel:UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel x:Name="dp">
<TextBlock x:Name="NameTextBlock" Text="testtesttest" DockPanel.Dock="Left"></TextBlock>
<Button x:Name="ThaButton" Content="button" DockPanel.Dock="Right"></Button>
</DockPanel>
</UserControl>
Hopefully I've explained my problem well enough. I might have taken the wrong approach to this problem, let me know if there's a better way.
Cheers
Remove :UserControl from x:Class="WPF_OOM.ContactPanel:UserControl" so it looks like x:Class="WPF_OOM.ContactPanel"
Also in your code behind you have the ContactPanel inheriting form ContentControl, but in your xaml it inherits from UserControl. These base classes must match. So change them both to be UserControl or ContentControl
It looks like you created a separate .cs file? I would recommend working with the code behind generated with the xaml file. One way to get to it is in the xaml editor right-click and choose 'View code'. If you imported the xaml and .cs file then reloading the project or restarting visual studio should combine them again in solution explorer if they are not already.
I would suggest looking into the Model-View-ViewModel or MVVM pattern. It works rather nice with WPF ;)
Hope this helps get you started.
I am using GraphX in Winform project. I am trying to display labels besides the edges. I want to know what property do I have to set in order to display some text in the label.
I have tried setting the 'Text' property of DataEdge, and then calling
ShowAllEdgesLabels(true);
but it does not work this way. Going through the forums I have found that WPF has a way to bind this property to the visual control. The XAML code is as follows
<gxl:EdgeLabelControl x:Name="PART_edgeLabel" Content="{Binding Edge.Text, RelativeSource={RelativeSource TemplatedParent}}" />
Now the question is what is the equivalent of Winform to achieve this functionality.
I found a solution with the help of the admin at the host of GraphX (PantheR).
Basically, we need to add the hostControler for WPF in a windows form.
We need to add a custom XAML template in the resources folder.
We need to load the XAML as a new resource in the code, before we initialize the graph.
We need to add a line of code to merge the resources.
Then in the XAML code we do the binding as mentioned in the question. The code has been updated at the repository to reflect these changes.
The downfall of this solution is that, we need to provide a XAML resource file with the program, but thats just another resource (in my opinion).
For anyone that need some reference code from #ResVic's answer:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graphx="http://schemas.panthernet.ru/graphx/"
xmlns:local="clr-namespace:YOUR_NAME_SPACE">
...
<Style TargetType="{x:Type graphx:AttachableEdgeLabelControl}">
<Setter Property="ShowLabel" Value="False" />
</Style>
...
</ResourceDictionary>
The Show case demo is protentially helpful for figuring out what stuff the lib could do and how to tweak it to work.
I need to an extra dependancy property to a control, so I'm overriding it. I.E.:
namespace Custom_TextBlock_Sample
{
public class CustomLabel: Label
{
}
}
But I seem to be unable to add it to a DataTemplate. The following code will fail to build:
... xmlns:Custom_TextBlock_Sample="clr-namespace:Custom_TextBlock_Sample" ...
<DataTemplate x:Key="Test">
<Grid>
<Custom_TextBlock_Sample:CustomLabel></Custom_TextBlock_Sample:CustomLabel>
</Grid>
</DataTemplate>
However inserting my overriden control anywhere else, (say not in the datatemplate) .. and compilation works no problem.
The following works: (My control is not nested in a date template).
<Grid>
<Custom_TextBlock_Sample:CustomLabel></Custom_TextBlock_Sample:CustomLabel>
</Grid>
Also using a regular label in the Datatemplate also works:
<DataTemplate x:Key="Test">
<Grid>
<Label/>
</Grid>
</DataTemplate>
Any ideas on what the issue might be ? Can I simply not add overridden controls to datatemplates in such a manor ? Is this a bug in Visual Studio 2007?
Note that since my intended changed to the control will be small, I simply want to override "Label" instead of wrapping it in a UserControl.
Thanks
Ok, figured it out. This seems to be a namespace bug with Visual Studio. The problem fixed itself, when I started a new project from scratch, this time with no spaces in the project name and no "_" in namespaces names.