|
I am using C# Express 2010 on an XP box targeting .Net 4.0.
I have been following some examples from an article in MSDN Magazine[^]
At design time the ListBoxItems are visible, but at run time the ListBox is empty.
Here is the code, this is it, there's no code behind.
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="MoreColours" XPath="/colours">
<x:XData>
<colours>
<colour name="pink"/>
<colour name="white"/>
<colour name="black"/>
<colour name="cyan"/>
<colour name="gray"/>
<colour name="magenta"/>
</colours>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Margin="0, 5, 0, 0" Text="Colours:" TextWrapping="Wrap"/>
<ListBox x:Name="lboxColour" Width="248" Height="56" Margin="0,0,0,3" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource MoreColours}, XPath=colour/@name}"/>
<TextBlock Width="248" Height="24" Margin="0,5,0,0" Text="You selected colour:"/>
<TextBlock Width="248" Height="24" Text="{Binding ElementName=lboxColour, Path=SelectedItem.Content, Mode=OneWay}"
Background="{Binding ElementName=lboxColour, Path=SelectedItem.Content, Mode=OneWay}"/>
</StackPanel>
The results are the same if I extract the XML to an external File, as here
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="Colours" Source="Colours.xml" XPath="/colours" />
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Margin="0, 5, 0, 0" Text="Colours:" TextWrapping="Wrap"/>
<ListBox x:Name="lboxColour" Width="248" Height="56" Margin="0,0,0,3" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource Colours}, XPath=colour/@name}"/>
<TextBlock Width="248" Height="24" Margin="0,5,0,0" Text="You selected colour:"/>
<TextBlock Width="248" Height="24" Text="{Binding ElementName=lboxColour, Path=SelectedItem.Content, Mode=OneWay}"
Background="{Binding ElementName=lboxColour, Path=SelectedItem.Content, Mode=OneWay}"/>
</StackPanel>
Does anyone have any idea why this might be. I am pretty sure that I have the code as in the article and I have asked a few days ago at the address given in the article, but have had no reply.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec.
Business Myths of the Geek #4 'What you think matters.'
modified on Wednesday, June 30, 2010 6:51 PM
|
|
|
|
|
I'm not sure why your XML data island x:XData isn't working, but the second version has to be able to find the XML file in the same directory as the executable. Either set the properties on it to copy to the output directory, or give it the full path, i.e
.<StackPanel.Resources>
<XmlDataProvider x:Key="MoreColours" XPath="/MyColours" Source="C:\Mef\WpfXmlSample\WpfXmlSample\Colours.xml" />
</StackPanel.Resources>
[Edit]Deliver a slap sideways to my head. The reason your XData isn't being picked up is because it's trying to resolve against a namespace (i.e. you need to supply a namespace to map against the XPath). To get round this, add xmlns="" to the x:XData declaration (i.e. <x:XData xmlns=""> ).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks Pete. The empty namespace tip worked for my first example.
As far as the second (XML File) example the file is set to 'copy if newer' and it is definitely in the same directory as the exe. Before I posted I had already tried with it set to Build action of 'None' and 'Resource', neither of which worked.
If you, or anyone else, have any other ideas, I'd be glad to hear them.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec.
Business Myths of the Geek #4 'What you think matters.'
|
|
|
|
|
I ran your sample with the following code and it works with this:
<Window x:Class="WpfXmlSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="MoreColours" XPath="/MyColours" Source="Colours.xml" />
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Margin="0, 5, 0, 0" Text="Colours:" TextWrapping="Wrap"/>
<ListBox x:Name="lboxMyColour" Width="248" Height="80" Margin="0,0,0,3"
ItemsSource="{Binding Source={StaticResource MoreColours}, XPath=MyColour}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Mode=OneTime, XPath=@name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks for your help Pete!
Between that and a little experimentation I have managed to get it working.
Just in case you are interested, here's the code:
<Window x:Class="Binding_to_XML_File.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="Colours" XPath="/colours" Source="Colours.xml" />
<XmlDataProvider x:Key="MoreColours" XPath="/colours">
<x:XData>
<colours xmlns="">
<colour name="pink" primary="true"/>
<colour name="white" primary="false"/>
<colour name="black" primary="true"/>
<colour name="cyan" primary="false"/>
<colour name="gray" primary="true"/>
<colour name="magenta" primary="false"/>
</colours>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Margin="0, 5, 0, 0" Text="Colours:" TextWrapping="Wrap"/>
<ListBox x:Name="lboxColour" Width="248" Height="56" Margin="0,0,0,3" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource Colours}, XPath=colour/@name}">
</ListBox>
<TextBlock Width="248" Height="24" Margin="0,5,0,0" Text="You selected colour:"/>
<TextBlock Width="248" Height="24" Text="{Binding ElementName=lboxColour, Path=SelectedValue, Mode=OneWay}"
Background="{Binding ElementName=lboxColour, Path=SelectedValue, Mode=OneWay}"/>
</StackPanel>
</Window>
Now by changing ItemsSource="{Binding Source={StaticResource Colours}, XPath=colour/@name}" to ItemsSource="{Binding Source={StaticResource MoreColours}, XPath=colour/@name}" I can use the file or embedded XML
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec.
Business Myths of the Geek #4 'What you think matters.'
|
|
|
|
|
Job done. Of course, you could also have consulted my article here[^] which demonstrates how to do something like this (without the XData side).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hmmm. We seem to have attracted a univoter. Let me balance that out.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
It is being a busy little 'b' isn't it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec.
Business Myths of the Geek #4 'What you think matters.'
|
|
|
|
|
Chris has hunted down and delivered a beeyitch slapping.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi, All
i have VS2008 in my PC. Now i want to make a application in silverlight. I am unable to work with silverlight.
Acctually i have seen in the tutorials that we need to select silver light project from the new project window but i didn't find the same.
I have installed the silverlight by downloading it from silverlight.net but still but able to create the silverlight project.
I got a link for silverlight_Alpha for the same i install the same also, finally i got the silverlight template, but the problem remains the same when i tried to create a project i got a error "The project type is not supported by this installation"
So plz help me out. Plz tell me what to install for silverlight, from where i can download the same.
Thanks
Yogesh Agarwal
|
|
|
|
|
Since you are working with VS 2008, I'm assuming you must be installing Silverlight 3.
Just the SL runtimes are not enough for development.
You need the SDK + silverlight tooks (for VS 2008) which are included in the SL tools setup[^].
You will also need VS 2008 SP1.
|
|
|
|
|
you need to install Silverlight 3 SDK for vs 2008. not silverlight 4!!
|
|
|
|
|
souidi abderrahmen wrote: you need to install Silverlight 3 SDK for vs 2008. not silverlight 4!!
Correct. Since the OP is using VS2008, he can only use SL2 or SL3 and not SL4. This requires VS 2010.
|
|
|
|
|
that is right because i did the deploy on vs 2010, silverlight 4.
|
|
|
|
|
Hi Yogesh,
First of all, as you are using Visual Studio 2008 you can develop only Silverlight 3 applications using the IDE. Visual Studio 2008 doesn't support Silverlight 4. If you use Visual Studio 2010, you can develop both Silverlight 3 and Silverlight 4.
Assuming you want Silverlight 3 using Visual Studio 2008. For this, you need to install the Visual Studio 2008 SP1 (Service Pack 1). Once you upgraded to SP1, you need to download and install the Silverlight Tools for Visual Studio 2008 SP1.
Once installed properly, you will be able to create a new Silverlight project using your Visual Studio IDE.
Let me know, if you need more help.
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial
|
|
|
|
|
For vs2008 :
we have to install first in this serial order
1)xp service pack 3
2)SQL Server
3)VS 2008
4)VS2008 Service Pack1
5)Silverlight Tools
Cheers
Ch Gayatri
|
|
|
|
|
I am trying to animate the FontSize and the rotation of a TextBlock. The FontSize animates but I can not get the
text to rotate. Here is the XAML
<Window x:Class="WpfSameGame.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="550" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded" Closing="Window_Closing">
<Window.Resources>
<DoubleAnimation
x:Key="StringFontSize"
To="20"
AccelerationRatio=".5"
DecelerationRatio=".5"
Duration="0:0:02"
Storyboard.TargetName="gameOver"
Storyboard.TargetProperty="FontSize"
/>
<DoubleAnimation
x:Key="StringRotation"
To="360"
AccelerationRatio=".5"
DecelerationRatio=".5"
Duration="0:0:2"
Storyboard.TargetName="gameOver"
Storyboard.TargetProperty="TextEffects[0].Transform.Angle"
/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" FontFamily="comic sans ms" FontStyle="Italic"
Foreground="Red" FontSize="20" HorizontalAlignment="Center"
VerticalAlignment="Center" Name="textBlk">
<Run FontSize="10" Name="gameOver" Text="" />
<Run>Score: </Run>
<Run Name="scoreText">0</Run>
</TextBlock>
<Button Width="70" Height="25" Click="NewGame_Click"
VerticalAlignment="Center" HorizontalAlignment="Right">NewGame</Button>
<UniformGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
Rows="12" Columns="12" Width="504" Height="504" Name="gameGrid" Background="Cyan">
</UniformGrid>
</Grid>
</Window>
Here is the relevant code behind
DoubleAnimation fsa = this.FindResource("StringFontSize") as DoubleAnimation;
DoubleAnimation sra = this.FindResource("StringRotation") as DoubleAnimation;
Storyboard sb = new Storyboard();
TextEffect te = new TextEffect();
te.Transform = new RotateTransform(0);
this.gameOver.Text = "Game Over ";
gameOver.TextEffects.Add(te);
sb.Children.Add(sra);
sb.Children.Add(fsa);
sb.Begin(this.textBlk);
|
|
|
|
|
You need to set the PositionCount of the TextEffect. te.PositionCount = this.gameOver.Text.Length;
|
|
|
|
|
Setting the "PositionCount" solved the problem. Thanks.
Now a second problem. With the animations defined as a window
resource, they run one time only. What do I have to reset in a
DoubleAnimation to reuse it?
For now I am creating the animations at run time. But I would
like to know how to reuse an animation.
|
|
|
|
|
Hi Experts,
I wanted to get the SelectedText on edit mode in WPF datagrid (i.e Active cell can be entire text or partial text selected by user within datagrid)
SelectedLength(Length of the selected text in datagrid cell), Selected Position (cursor position in datagrid cell). How these all values can be achieved in WPF datagrid. For better understanding please read the below example.
For Example: suppose user has double clicked datagrid in row 1 and col 2 position, data available on the selected position is Text. Consider user has selected only ext out of (Text). So my expected result should be
Selected Text: ext
Selected Position: 1
Selected Length: 3
Cell Text: Text
Thanks in advance
|
|
|
|
|
kartheesh wrote: Selected Text: ext
Selected Position: 1
Selected Length: 3
Cell Text: Text
A textbox provides these properties - not a text block.
A datagrid is composed of textblocks as cell components so this may not be possible until you style the datagrid differently.
|
|
|
|
|
If I was looking to do something more complex like this, I would consider rolling my own Blend behaviour or DependencyProperty that did this, and hook it up to the textboxes in the editable template. I would favour the Blend behaviour here as it is a lot easier to set up, and you are just going to be extending the TextBox control for this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi Experts,
Appreciate if anybody can help me out with small code snippet.
Thanks in advance.
|
|
|
|
|
To give you an idea what a Behavior looks like, he's one I put together for selecting all text when a textbox gets focus. You will want to consider creating a variation on this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace Goldlight.Extensions.Behaviors
{
public class SelectAllTextBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(AssociatedObject_GotKeyboardFocus);
AssociatedObject.GotMouseCapture += new System.Windows.Input.MouseEventHandler(AssociatedObject_GotMouseCapture);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotKeyboardFocus -= new System.Windows.Input.KeyboardFocusChangedEventHandler(AssociatedObject_GotKeyboardFocus);
AssociatedObject.GotMouseCapture -= new System.Windows.Input.MouseEventHandler(AssociatedObject_GotMouseCapture);
}
void AssociatedObject_GotMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
{
AssociatedObject.SelectAll();
}
void AssociatedObject_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
AssociatedObject.SelectAll();
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks for the reply buddy,exactly what my requirement is i will explain you.
I have created mdi form in WPF with few menus like copy,cut,paste and in my active form i have datagrid control.
Using copy,cut and paste functionality from the menu options i wanted to modify the datagrid cell data. As of now to implement the same i am doing workaround by refering the below link:
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-clipboard
Appreciate to get help in this regards.
Thanks & Regards
Kartheesh
|
|
|
|
|