|
ListBoxItem item=listbox1.selectedItem();
label1.content=item.ToString();
Solution of problem
modified on Sunday, August 23, 2009 11:00 AM
|
|
|
|
|
|
Greetings,
I'm trying to throw together a quick silverlight proof of concept, but I'm stuck on a slight issue.
Let's say I have a base class (we'll call it Mammal) and two derived classes (dog and cat, respectively). I have an ObservableList<Mammal> that holds a whole bunch of cats and dogs.
Now, I have a listbox defined like so:
<ListBox x:name="lstMammals">
...
</ListBox>
A little ways further down, I have a ContentControl (I'm not sure if this is the right control to use here) that contains a StackPanel with two user controls in it. One of the user controls handles dogs and the other cats. How do I do the binding in XAML in such a way that when I select an item in the listbox that the appropriate user control pops up for editing it?
Or am I looking at the problem wrong?
Thanks,
Will
|
|
|
|
|
It sounds like you want a master/detail scenario...
Unfortunately, Silverlight doesn't support the rich, flexible data template
bindings to do this like WPF has, so you'll need a little code.
You can handle the listbox's selectionchanged event, and in your
handler show a detail control appropriate for the type of the selected item.
A search on "Silverlight master detail" yields several examples, like this
one: Using User Controls to Implement Master/Detail Scenarios[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This is one for you WPF gurus. Basically, I want a button to pulsate continuously (i.e., fade from one color to another and back, then repeat forever). I've got that working, but it does not work under another condition. I also want to be able to change styles at runtime. I have the runtime style changing working and I have the pulsating button working, but not both at the same time. The problem seems to be that I'm using Button.Loaded as the event trigger that begins the storyboard. However, if I swap the style after the button has already loaded, that storyboard never starts, so the animation doesn't execute. I was thinking there was perhaps another event I could use (such as an "on style switch" event), but I'm really at a loss. Here is some simple code to help you understand the problem:
First, I have a resource dictionary stored in "AnimatedStyleDictionary.xaml". This contains the style for the pulsating button, as shown below:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Button.Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush x:Name="rectFill" Color="Yellow" />
</Rectangle.Fill>
</Rectangle>
<ContentPresenter Content="{TemplateBinding Button.Content}" />
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="rectFill"
Storyboard.TargetProperty="Color"
To="Blue"
AutoReverse="True"
RepeatBehavior="Forever"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Next, I have a Window with a button on it.
<Window x:Class="CodeProjectSample_RuntimeSwapAnimatedStyle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Button Click="Button_Click">Hello</Button>
</Window>
When that button gets clicked, the application style gets switched (I know this is a poor technique, but I wanted to trim the code to a minimum for this sample):
private void Button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
}
The above code is where the failure occurs. The style gets applied (I can confirm because the button turns yellow), but the animation does not fire. Also, I have confirmed that the animation does work. If I modify the code to load the style in the Window constructor rather than the button, the animation works fine. However, that is not a working solution, as I want the user to be able to change between styles at runtime. Here is the code that causes the animation to work:
public Window1()
{
Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
InitializeComponent();
}
Any help you could provide would be greatly appreciated!
Learn as if you would live forever, live as if you would die tomorrow.
modified on Thursday, September 3, 2009 6:29 PM
|
|
|
|
|
Did you try exposing an custom event that gets raised when the style changes? Then your event can fire when you app needs it to.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Great idea, that should cut my programming effort down about 100x. However, that still means I need to have every Window/Page subscribe to that event. Do you know of any way to force an event to fire on every Window/Page of a WPF application? Or, is there some way for an EventTrigger to subscribe to an event that does not get routed through the control that it is attached to (e.g., a static event on a class)? I'd rather cut my effort down by 1000x than "just" 100x
Visual Studio is an excellent GUIIDE.
|
|
|
|
|
Why not have the style subscribe to the event? Instead of Loaded, subscribe to MyStyleChanged event?
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Can you post an example of how I might do that (some code, or perhaps an article)? At least through event triggers, I only know how to subscribe to events that actually get routed through the control (in this case, a button) the event trigger is being applied to. I'm not sure how I would go about subscribing to an event on a static class (such as MyApplication.StyleChanged).
Visual Studio is an excellent GUIIDE.
|
|
|
|
|
I may have figured out a solution, although it's a little sloppy. I'll post the code once I get it tested, but here's the idea:
- Create a custom control. Let's call this control "StyleChangedControl".
- StyleChangedControl will expose a custom event, StyleChanged.
- When a StyleChangedControl constructor is called, it will subscribe itself to the static event, StyleChangedControl.ApplicationStyleChanged. We'll call this new StyleChangedControl "NewStyleChangedControl".
- When I change the application style, I will call StyleChangedControl.NotifyOfStyleChange() (a static method).
- NotifyOfStyleChange() will fire the StyleChangedControl.ApplicationStyleChanged event (each NewStyleChangedControl will receive notification of that event).
- Once NewStyleChangedControl gets notification of the ApplicationStyleChanged event, it will itself raise its own StyleChanged event.
- In the resource dictionary that declares the style that contains the button, there will be an instance of StyleChangedControl.
- The button will have an event trigger that is subscribed to that NewStyleChangedControl's StyleChanged event.
- When that StyleChanged event gets fired, the event trigger will recognize it, and will then initiate an animation. This will accomplish the goal of firing an animation when the application style changes.
Effectively, I create a custom control that has a custom event that gets fired by the custom control class. There is a static method on that custom control class that allows another class to force the event to fire. Every single instance of the custom control will then receive that event. That event will be recieved by the event trigger in the style and will initiate the animation.
Anyway, that's all very confusing and it seems there must be an easier way. I'm not even sure it will work. I'll get back to you when I have some code to present. In the meantime, feel free to poke holes in the above process
Visual Studio is an excellent GUIIDE.
|
|
|
|
|
Seems overly complex. Give this a try.
In the OnStyleChnaged method you can start and stop your storyboards as needed.
Public Class MyButton
Inherits Button
Protected Overrides Sub OnStyleChanged(ByVal oldStyle As System.Windows.Style, ByVal newStyle As System.Windows.Style)
MyBase.OnStyleChanged(oldStyle, newStyle)
End Sub
End Class
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Hmm, I'm not sure that will work, as you don't have access to the animation from the MyButton.OnStyleChanged method. Also, I'm not quite sure I like the idea of inheriting from every control that I want to be able to animate on a style switch. However, I got my technique coded and it ain't pretty, but it works. Here is the code in all its horrific glory:
The resource dictionary in "AnimatedStyleDictionary.xaml":
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style TargetType="Button">
<Setter Property="Button.Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<local:StyleChangedControl x:Name="MyStyleChangeCon" />
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush x:Name="rectFill" Color="Yellow" />
</Rectangle.Fill>
</Rectangle>
<ContentPresenter Content="{TemplateBinding Button.Content}" />
</Grid>
<ControlTemplate.Triggers>
<EventTrigger SourceName="MyStyleChangeCon" RoutedEvent="local:StyleChangedControl.StyleChange">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="rectFill"
Storyboard.TargetProperty="Color"
To="Blue"
AutoReverse="True"
RepeatBehavior="Forever"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
The main window XAML (just a button really):
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Button Click="Button_Click">Change Style</Button>
</Window>
The C# code behind the main window:
public partial class Window1 : Window
{
public Window1() {InitializeComponent();}
private void Button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Add(
(ResourceDictionary)Application.LoadComponent(
new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
Thread t = new Thread(new ThreadStart(delegate()
{
this.Dispatcher.Invoke(new ThreadStart(delegate()
{
StyleChangedControl.NotifyOfApplicationStyleChange();
}));
}));
t.Start();
}
}
The magical control that sends notifications of application style updates:
public class StyleChangedControl : Control
{
private static event ThreadStart ApplicationStyleChange;
public event RoutedEventHandler StyleChange
{
add
{
AddHandler(StyleChangeEvent, value);
}
remove
{
RemoveHandler(StyleChangeEvent, value);
}
}
public static RoutedEvent StyleChangeEvent =
EventManager.RegisterRoutedEvent("StyleChange",
RoutingStrategy.Direct, typeof(RoutedEventHandler),
typeof(StyleChangedControl));
public static void NotifyOfApplicationStyleChange()
{
if (ApplicationStyleChange != null)
{
ApplicationStyleChange();
}
}
public StyleChangedControl()
{
this.Visibility = Visibility.Collapsed;
ApplicationStyleChange += this.OnStyleChange;
}
private void OnStyleChange()
{
RoutedEventArgs e = new RoutedEventArgs(StyleChangeEvent);
RaiseEvent(e);
}
}
Like I said, it ain't pretty, but it works. Since I figured out my own solution, I'm going to mark this as solved. Thanks for all your ideas, Karl!
Visual Studio is an excellent GUIIDE.
|
|
|
|
|
While setting up a service reference for my silverlight app I keep getting the warning:
Web services at 'webserver' might not be accessible due to cross-domain restrictions:
For more details please contact the web server administrator or press F1 after selecting this warning
I've setup both a clientaccesspolicy.xml and crossdomain.xml in the web sites root directory. As far as I can tell they are configured correctly. But for some reason my Silverlight app seems to be either not seeing them or ignoring them.
If I browse to the file off my web root they are visible http://[URL]/crossdomain.xml etc.
Are there any secret ninja setting for getting this to work correctly? Web site permissions or script access settings?
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*" />
<domain uri="https://*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
|
|
|
|
|
Apparently visual studio was the name of my pain. When I setup my Service Reference, Visual Studio changed that URL it was actually supposed to be pointing to in my ServiceReferences.ClientConfig file.
So instead of pointing to <endpoint address="http://[IP Address]/SilverlightExample/CommunicationService.svc" that it was supposed to.
Visual Studio tried to point to: <endpoint address="http://[Machine Name].local/SilverlightExample/CommunicationService.svc"
Why on Earth it decided to re-write the URL I gave it for the service I have no idea.
|
|
|
|
|
Hello,
I'm an experienced programmer and I need to write a program capable of displaying a music sheet on screen.
My problem resides on the technology used for display. I first thought of OpenGL as I have experience with it and the TAOFramework that makes the use of OpenGL possible on .NET.
Then, well I have extensive knowledge of windows forms and all but I'm not really up to date on the latest versions of the framework so I think it could be done with WPF Microsoft marketted as wonderful.
So; my question for those still reading: could I write a fast, customm controller capable of displaying music sheet using WPF and if so could someone point me to some good starter page?
Thank you!
|
|
|
|
|
If it needs to be fast, I wouldn't use .Net at all...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
by "fast" i mean that I don't want it to lag when a user uses the scrollbar because it has to redraw. Anyway .NET will give me a much shorter development cycle.
|
|
|
|
|
There are lots of superb examples of just about every aspect of WPF here on the Code Project. Search, Read, Wonder and Enjoy.
|
|
|
|
|
I am designing an UI Application with WPF (XAML and C#). This application interacts with Database(sqlserver 2008) too. This app is a tool for the project.
I have to implement this with CAB architecture and Model-View-Presenter(MVP).
If I have functionalites like 1. Uploading data to database 2. Downloading data from database. then,
My questions are : 1. What will Model, View and Presenter roles in these applications.
How the interfaces between these modules happen?
Is it I have to Create IModel (interface) b/w presenter and Model, IView (interface) b/w presenter and view?
So, my codebehind will have only events calls (like btn_click) ??
Which module handles Database access?
I know I asked lot of questions, but I have made the UI ready for the rest of the work flow .
I am sorry if many questions are put in one place. I will be so glad, if I can get answers for all the things.
Kindly help me.
Thank you,
Ramm
|
|
|
|
|
|
Hi,
In a WPF Application using XAML,
I have 2 images, which needs to be oriental of horizantal. One is of width 784 * 66 and other is 1 * 66.
while, design time, since my window is auto it shows properly, but during runtime, the window is of 1280 width... so, the image 1 * 66 should stretch and cover the rest (1280 - (784 + 1))
|____________________________|_|
should become
|____________________________|_______________________|
which means , my firstimage should have the same width (784) and the second image should stretch to cover the rest even though its only one pixel.
<Grid HorizontalAlignment="Left" Height="66" Name="grdTopImages">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" MinWidth="1" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="66" />
</Grid.RowDefinitions>
<Image Name="imgClientPhoto" Grid.Column="0" Grid.Row="0" Source="/Honeywell.eHTMP;component/Resources/_left.gif"></Image>
<Image Name="imgExtraImg" Grid.Column="1" Grid.Row="0" Stretch="Fill" Source="/Honeywell.eHTMP;component/Resources/_right.gif"></Image>
</Grid>
Please help me
Thanks
Ramm
|
|
|
|
|
Hi Guys,
I could solve it,
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" MinWidth="1" />
should be the other way width property
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="1" />
Thanks a lot
Ramm
|
|
|
|
|
Hello,
I have a Silverlight 3 application where I use a tab control and I want to implement the navigational framework into it. The tabs on my page are created dynamically by the user, saying this
TabItem ti = new TabItem();
Frame frame = new Frame();
ti.Content = frame;
MyTabs.Items.Add(ti);
When the user clicks a link, I want content to load into whichever tab is currently selected, so I say this
HyperlinkButton btn = sender as HyperlinkButton;
string url = btn.Tag.ToString();
Frame frame = MyTabs.SelectedContent as Frame;
frame.Navigate(new Uri(url, UriKind.Relative));
The current tab loads the page into its frame correctly. However, as I click through the other tabs, they also have loaded the page into their frames, so all open tabs have the exact same content. So for example, the user has 5 tabs open. They click a link, and the currently selected tab loads a Page into its Frame. If they go to one of the four other tabs, they see that those tabs also loaded the same Page. How can I make sure that only the selected tab gets its new Page and not the other tabs? Thanks to anyone who can help.
|
|
|
|
|
Have you tried using Prism V2 (CompositeWPF)? There is a TabRegaion Adapter that you can use.
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
Microsoft MVP (Silverlight), WPF/Silverlight Insiders
|
|
|
|
|
Hi,
I am creating a Windows application (WPF) and C#. In my view, I have to add few layouts like browsing a folder, displaying the files in the folder in a list view...etc
My requirement is :
The panels mentioned above should be collapsible panels, I guess, we dont have option of collapsible panel in wpf.
I have to create a custom control for this? If so, Please suggest me how to do this?
Thank You,
Ramm
|
|
|
|
|