|
Then don't use references that are null
On a more serious note - what are you using that's null in an
event handler?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Very funny....
If I set the first expander to a selectedindex = -1 then when I go retrieve an object from a different expander I get the nullreference exception stating Object Reference to set to instance of an object.
See I have 3 listboxes and they hold a reference to the name portion of Collection<person> object.
I have a generic_Selectionchange event handler that casts the selected item back to the person object when ever there is a selection change in the listbox
lblPersonId.Content = ((personData.Person)((ListBox)sender).SelectedItem).personId;
If the selectedindex is set to -1 then whenever that listbox regains focus it throws the error.
Terrance C.
|
|
|
|
|
Tee123 wrote: If the selectedindex is set to -1 then whenever that listbox regains focus it throws the error.
Right....I don't understand why you don't check for that condition and take the
appropriate action - you should have been doing that anyway, right?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm new in WPF programming. I created a custom window template and I created the code behind class as a ResourceDictionary. I can modify my windows layout using Style="{DynamicResource MyCustomWindow}
Everything works good, but now I added two buttons Save and Exit and I want generate two different behaviors, but the ButtonClick evente are in the template code behind and I don't know how to raise an evenmt or call a method in the host window.
Thanks.
Armando
|
|
|
|
|
Use either Routed Event or ICommand class for the same.
Niladri Biswas
|
|
|
|
|
I have a togglebutton template which have vsm to do something on checked, pressed etc.
I want to add Polygon inside that template and add storyboard to it....
I want to add it from .cs file...so that I can change the coordinates of polygon dynamically as the size of the button content....
How can I do that?
|
|
|
|
|
Did you take a look at the PathGeometry object?
You could generate the polygon in blend and then assign it through code (i.e. your cs file).
|
|
|
|
|
Do u have any code snippet/link to just give me some idea and make things clear?
Also one thing to note that this polygon should be inside buttton template so that I can give the animation?
|
|
|
|
|
|
Hi..
I am using hierarchical data template in tree view in my wpf application using mvvm pattern.
Binding of data using observable collection done successfully through viewmodel..
But my problem is in getting selected item..
Can anybody tell me how to get selected item in treeview in mvvm pattern.....?
|
|
|
|
|
Try reading Josh's article on MVVM with the Treeview[^]. Download the code and see how Josh does it.
"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
|
|
|
|
|
I only tested this in Silverlight, should be the same in WPF though.
You can in fact bind to SelectedItem on the TreeView
<controls:TreeView ItemsSource="{Binding KeywordSource}" SelectedItem="{Binding SelectedKeyword, Mode=TwoWay}">
Blend throws an error, noting (correctly) that SelectedItem has not have an accessible setter, which is also the reason you will not get the SelectedItem in Intellisense. If I understand correctly however, you just need the getter, which is working for me.
|
|
|
|
|
Setting the Mode on the Binding of the SelectedItem to OneWayToSourceeliminates the error message
<controls:TreeView ItemsSource="{Binding KeywordSource}" SelectedItem="{Binding SelectedKeyword, Mode=OneWayToSource}">
I lied, throws a compile time error...
|
|
|
|
|
I have been struggling with this issue for some time, and I have now constructed a simple example to demonstate the problem. I am not sure whether this is an issue with WPF or my lack of understanding.
As part of our application, I have created a wizard along the lines of Creating an Internationalized Wizard in WPF. This uses the MVVM pattern and the various pages of the wizard are implemented in User Controls.
The problem that I have is when the wizard window is resized, the UserControl does not take the height of the window. However it does take the width of the window. The result is that controls such as a list box, which the user may need to have resize with the window, do not resize.
I have removed the complexity of our app, and created a very simple example with a window, that just contains a HeaderedContentControl, which contains the model for the UserControl.
The window is
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UserControlDemo"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate DataType="{x:Type local:SimpleUserControlModel}">
<local:SimpleUserControl />
</DataTemplate>
</Window.Resources>
<Grid>
<HeaderedContentControl Name="hcControl" Margin="20" Content="{x:Type local:SimpleUserControl}"
Height="auto" Width="auto" VerticalContentAlignment="Stretch" />
</Grid>
</Window>
Window code:
Class Window1
Private _simpleModel As SimpleUserControlModel
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_simpleModel = New SimpleUserControlModel
hcControl.Content = _simpleModel
End Sub
End Class
User control:
<UserControl x:Class="SimpleUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid>
<Border Margin="5" BorderThickness="5" BorderBrush="Red" />
</Grid>
</UserControl>
User control model:
Public Class SimpleUserControlModel
End Class
The UserControl just contains a red border, which makes it easy to see its size. If you run the application and drag the corner of window1, the width of the red border changes, but its height remains at 0. Checking the height (in fact it is the ActualHeight property) of the HeaderedContentControl, it is set as expected (about 260) - I had expected the UserControl would inherit the value of this property, so its height would be set automatically.
It would be greatly appreciated if someone could explain what I am missing here...
Please note that a workaround is relatively easy and has been implemented in our app: add a property for the required height to SimpleUserControlModel then bind the UserControl height to the property. Then by catching the Window's SizeChanged event, the property can be updated (its not quite that simple, because you also need a timer to add a delay to avoid handling every window SizeChanged event).
Thanks for any help.
Tim
|
|
|
|
|
The default template for a HeaderedContentControl is
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<StackPanel>
<ContentPresenter ContentSource="Header"/>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
which gives you the behavior you're seeing, because they used a StackPanel.
I would use a grid in the HeaderedContentControl template instead.
Here's an example (simplifying your example even more):
<Window.Resources>
<Style x:Key="HeaderedContentControlStyle1" TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" ContentSource="Header"/>
<ContentPresenter Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<HeaderedContentControl Name="hcControl" Margin="20" Style="{DynamicResource HeaderedContentControlStyle1}" >
<Grid>
<Border Margin="5" BorderThickness="5" BorderBrush="Red" />
</Grid>
</HeaderedContentControl>
</Grid>
Mark Salsbery
Microsoft MVP - Visual C++
modified on Tuesday, May 19, 2009 12:48 PM
|
|
|
|
|
Mark,
Thanks for the simple solution to this problem! I can see now that I needed to have a style with a control template to define the behaviour of the HeaderedContentControl. Reading the help page again, I can see I missed "A HeaderedContentControl has a limited default style. An application developer can create a HeaderedContentControl, but its appearance will be very simple."
Note that we need to use a UserControl, to represent a number of more complex "pages", which are swapped in / out in code as the user steps through the wizard. That is why we need the user control rather than the simpler approach you presented.
I will implement this back in our application and hopefully it will solve the original problem.
Regards,
Tim
|
|
|
|
|
ausadmin wrote: That is why we need the user control rather than the simpler approach you presented.
Right, I figured that. I just wrapped it all into simple XAML
so you could copy/paste it and try it out.
The key point was the custom template
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you for saving my day!
|
|
|
|
|
Hello all,
Basically i am trying to format single word in textbox. For eg. if user types 'Or' , 'and' i want to hilight them and bold them. This i am doing by extending textbox control and overriding OnRender. I could able to do that, but the problem is the cursor position is getting messup, in other words, when words are becoming bold cursor position is not getting adjusted accordingly, and am not sure how to do that. Here is my code
<br />
protected override void OnRender(DrawingContext drawingContext)<br />
{<br />
<br />
if (this.Text != "")<br />
{<br />
this.Background = new SolidColorBrush(Colors.Transparent);<br />
<br />
FormattedText formattedText = new FormattedText(<br />
this.Text,<br />
System.Threading.Thread.CurrentThread.CurrentCulture,<br />
FlowDirection.LeftToRight,<br />
new Typeface(this.FontFamily,this.FontStyle, this.FontWeight,this.FontStretch),<br />
this.FontSize, Brushes.Black<br />
);
<br />
<br />
double leftMargin = 4.0 + this.BorderThickness.Left;<br />
double topMargin = 2 + this.BorderThickness.Top;<br />
<br />
formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0);
drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)));
<br />
<br />
List<Pair> ranges = dec.Ranges(this.Text);<br />
foreach (Pair p in ranges)<br />
{<br />
formattedText.SetForegroundBrush(Brushes.Blue, p.Start, p.Length);<br />
formattedText.SetFontWeight(FontWeights.Bold, p.Start, p.Length);<br />
<br />
}<br />
<br />
<br />
drawingContext.DrawText(formattedText, new Point(leftMargin - this.HorizontalOffset-2,topMargin));<br />
}<br />
}<br />
Any help would be appreciated.
Thanks.
|
|
|
|
|
I'm not sure how you expect the TextBox to know about any formatted text
you've rendered.
TextBox doesn't support formatted text - RichTextBox does.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
Hi guys,
I want to start doing some samples in Silverlight. Was just wondering whether i shud have knowledge in ASp.net, for getting started in Silverlight. Pls give me a hint, from where shud i begin.??
Thnkx
Sandeep
The name is Sandeep
|
|
|
|
|
You might want to buy a book or two. My good friend Laurent Bugnion has written a rather good one called Silverlight 2 Unleashed[^].
"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
|
|
|
|
|
Try Jesse Liberty's Tutorials[^]
These are in both C# and VB. If you're a VB developer one of the problems is that the vast majority of snippets on the web are in C#.
Re: buying a book, one thing to be aware of is that Silverlight 3 books will be appearing in the late summer so I would avoid buying a book now unless you have to.
Again, if you're a VB developer then APress's "Pro Silverlight 2 in VB" (there's a C# version as well) looks good and it's also in colour. But the Silverlight 3 versions are already listed!
Some knowledge of ASP.NET helps but is not essential.
Kevin
|
|
|
|
|
thanks guys..
The name is Sandeep
|
|
|
|