|
Hi everybody
I have a problem, for which, I don't seem to find the answer
I Created a User Control, in wpf. In that control I defined a Dependency Property as(all the smaller than signs are replaced with '[', and grater than signs are replaced with ']'):
public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollections[SomeClass]), typeof(MyUserControl), new PropertyMetadata(new ObservableCollection[SomeClass]);
I also implemented the standard Getter / Setter for that property as
public ObservableCollection[SomeClass] Items
{
get{return (ObservableCollection[SomeClass])GetValue(ItemsProperty)};
set{SetValue(ItemsProperty, value);}
}
In the constructor of the user control I subscribe to the CollectionChanged event as:
public MyUserControl()
{
InitializeComponent();
Item.CollectionChanged += new NotifyCollectionChangedEventHandler(items_CollectionChanged);
}
and in the event handler I have the code that does what is needed
private void items_CollectionCHanged(object sender, NotifyCollectionChangedEventArgs e)
{
//Build control foreach item in e.NewItems and add them to UI and remove from UI controls foreach item in e.OldItems
}
I add UserControls to my window as follows:
[StackPanel]
[local:MyUserControl]
[local:MyUserControl.Items]
[local:SomeClass .... /]
[local:SomeClass .... /]
[/local:MyUserControl.Items]
[/local:MyUserControl]
[local:MyUserControl]
[local:MyUserControl.Items]
[local:SomeClass .... /]
[local:SomeClass .... /]
[local:SomeClass .... /]
[local:MyUserControl.Items]
[local:MyUserControl]
[/StackPanel]
From the start everything worked fine, but if I add two or more instances of MyUserControl, to the window, the first instance is OK, but the other one contains also the items that was defined in the first one. For the example above one of the UserControls whould have 2 instances of control that is build in the CollectionChanged event handler, and the other one would have 5 of them. Does anybody know how to solve this problem. Any advice will be appriciated!
Uroš
<div class="ForumMod">modified on Monday, April 14, 2008 9:33 PM</div>
|
|
|
|
|
The problem is that the dependency property is static, therefore the default value only gets initialized once and the same collection is used for all the controls. In the dependency property use the default null and create the collection in the constructor.
See http://msdn2.microsoft.com/en-us/library/aa970563.aspx
|
|
|
|
|
Great. That solved my problem. Thanks
|
|
|
|
|
hi,
anybody know how to get the photo gallery using silver light
plz reply me...
Rajendran.AL
|
|
|
|
|
Hello,
Have you tried using this project[^]? It seems pretty nice..
There are a lot of samples in Silverlight Gallery[^]. You can probably find the sample that you want.
|
|
|
|
|
thanks alot.
Rajendran.AL
|
|
|
|
|
Hi All,
I want to use DataGridView which should be editable in my WPF application.
In Form Base application we can simply use DataGridView But here i am
unable to use it, what cade i have to written in XAML.
So Please any body suggest me what i have to do to use DataGridView in WPF.
Thanks
Bankey
|
|
|
|
|
Bankey,
You can download and use distribute for free, the datagrid controls from eXceed or Infragistics.
You can also write you own, depending on how much functionality you require.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Karl Shifflett wrote: You can also write you own, depending on how much functionality you require.
It's surprising that more people don't do this - considering how easy it is to extend and restyle controls like the ListView.
|
|
|
|
|
If you don't want to download something like the Infragistics or Exceed grids (and really you should look at them - they are very good), you could always host the WinForms DataGridView.
If you're running VS 2008, the steps to do this are:
1. Reference System.Windows.Forms and WindowsFormIntegration in your project.
2. Add a WindowsFormsHost element to your XAML and put the grid inside
:<WindowsFormsHost Height="100" Width="200">
<wf:DataGridView x:Name="dg" />
</WindowsFormsHost> 3. Add the following declaration to your XAML:
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
|
|
|
|
|
Pete,
I'll be off line starting tomorrow and will be back on line 28 April.
I'll try and gets of pictures. I didn't get any at MIX08 so I guess I should for this trip.
Cheers!!!
Karl
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Karl Shifflett wrote: I'll try and gets of pictures. I didn't get any at MIX08 so I guess I should for this trip.
Cool. I'll try to hold the fort here while you're away Enjoy your trip.
|
|
|
|
|
Pete O'Hanlon wrote: I'll try to hold the fort
When they starting piling up, remember the Alamo! Do leave you post!! LOL...
Josh & I have classes together over there will be a great time!
Thanks!
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Karl Shifflett wrote: Josh & I have classes together over there will be a great time!
I'm jealous. I bet that's one hell of a time.
|
|
|
|
|
|
Hello,
I'm following Scott Guthrie's DIGG Silverlight tutorial as an introduction to Silverlight. I seem to have a problem debugging. When I run the solution, it works fine, but the process is not connected to the IDE. The break points aren't hit and I get a "no symbols loaded..." message. Any idea on why I can't step into the code behind?
Thanks!
Ian
|
|
|
|
|
- Open the application in VS.
- There will be two projects (SL and ASP.NET)
- Go to the properties of ASP.NET.
- Go to "Start Options"
- Check "Silverlight" in Debugger.
then, try to rebuild the application.
If it doesn't work, try to clear Temporary Internet Files. Actually, we don't need to clear those files but some said that this is a solution so you may try anyway.
|
|
|
|
|
Thanks for your reply!
As it turns out, the Silverlight debugger was turned on. I was using FireFox as the browser, so before I tried to delete the temp internet files, I decided to use Internet Explorer. Voila! It works! So I must have run into a bug regarding debugging a Silverlight project running in FireFox.
Thanks again!
Ian
|
|
|
|
|
Is is possible to use a Grid in the following situation:
<ListBox ItemsSource="{Binding ...}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Grid /> <!-- With necessary rows and columns -->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding Path=Text}" Grid.Row="{Binding Path=Row,Converter=...}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox> I've been trying the previous to no avail, the textblocks just won't sit in the appropriate row. I've got a feeling as to why this is happening but not how to solve it (note that using a UniformGrid without setting the rows and columns explicitly works).
Is it something to do with binding to an attached property?
|
|
|
|
|
Ed - sorry I haven't got round to looking at this before, but wouldn't a StackPanel with it's Orientation set to Horizontal do the job?
|
|
|
|
|
Nope, sorry. What I'm actually trying to do is create a schedule-like component (think Outlook week view). If you imagine the following class which stores the "Event information:
public class Event
{
public string Summary { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
} Then the following XAML:
<ListBox ItemsSource="{StaticResource Events}">
<ListBox.ItemTemplate>
<StackPanel
Grid.Row="{Binding Path=Start, Converter={StaticResource HourToRowConverter}}"
Grid.Column="{Binding Path=Start, Converter={StaticResource DayToColumnConverter}}"
Grid.RowSpan="{Binding Converter={StaticResource DurationToRowSpanConverter}}">
<TextBlock Text="{Binding Path=Summary}" />
<!-- Other stuff -->
</StackPanel>
</ListBox>
<ListBox.ItemsPanel>
<!-- Grid Goes here -->
</ListBox.ItemsPanel>
</ListBox> Hope that makes things a bit clearer, to be honest I was surprised when it didn't work but maybe it's something to do with the Grid.Row etc being an attached property? The UniformGrid works but I think that's something to do with the fact that you can just set the rows and columns and add children controls and they're each added to a cell in the order in which they appear in the XAML, whereas this does not happen with the normal Grid .
I'd really appreciate not having to go off and write a custom control just to manage what seemingly seems so simple.
|
|
|
|
|
Ed. I'm off for the weekend now, but I'll try to knock something together early on next week for you if that's OK with you.
Pete
|
|
|
|
|
Thanks, that'd be great, I'm not planning on doing anything on it over the weekend apart from go to a shoot tomorrow and then being re-wiring my Land Rover on Sunday.
To give you a bit more perspective on it, apart from learning what I had thought it'd be useful for would be something involved: WPF, LINQ, Visual Studio Tools for Office and plain old C#. I was thinking off getting an add-in for Outlook (or perhaps a service / tray app of some kind) to pull off by calendar from Outlook / Google Calendar (they're synced), use LINQ to select the current week (because it's so easy with that thing) and then use WPF to layout a view of the week like in outlook. Capture that to an image, do some fancy alpha blending with a background image and then set that as my desktop background.
Ambitious I know but hey , might be useful to someone else, would be interesting articles if I get the time.
I had thought of just using GDI+ for the rendering but when I came across a blog entry that said the Calendar Printing Assistant for Outlook uses WPF I thought what a good idea, much nicer for rendering it than loads of tedious calculations.
|
|
|
|
|
Have a look at this:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="OutlookWeekView.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480" Activated="Window_Activated">
<Window.Resources>
<XmlDataProvider x:Key="SummaryData" d:IsDataSource="True"
Source="D:\wpf\OutlookWeekView\Events.xml"/>
<DataTemplate x:Key="EventTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Padding="2"
Grid.Column="1"
Grid.RowSpan="2"
Text="{Binding Mode=OneWay, XPath=Summary}"/>
<TextBlock Grid.Row="0" Padding="2"
Grid.Column="0" Text="{Binding Mode=OneWay, XPath=Start}"/>
<TextBlock Grid.Row="1" Padding="2"
Grid.Column="0" Text="{Binding Mode=OneWay, XPath=End}"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ListBox ItemTemplate="{DynamicResource EventTemplate}"
ItemsSource="{Binding Mode=Default,
Source={StaticResource SummaryData},
XPath=/Events/Event}"/>
</Grid>
</Window> Does it help in any way?
|
|
|
|
|
I don't think I explained it well enough, what you've got works fine so to speak, if you set the Grid.Row and Grid.Column of the TextBlocks (in this instance) explicitly then it does arrange them in the appropriate "cell". However if you try and set them through binding then it doesn't work.
E.g.
<TextBlock Grid.Row="{Binding Path=Start,Converter={StaticResource DateToRowConverter}}"
Grid.Col="{Binding Path=Start,Converter={StaticResource DateToColConverter}}" Text="{Binding Path=Summary}" /> Where DateToRowConverter returns say the hour of the event and the DateToColConverter returns the day of week.
(Sorry hit the wrong key so you won't get the full post).
|
|
|
|