|
First thing, try to check if the link to the sample provided on the website runs on your machine.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
hello, I'm a WPF newbie
i have a class called Employee that has a single property Name, when i try to bind it to TextBlock, nothing happens...
what am i doing wrong ?
public MainWindow()
{
InitializeComponent();
Employee emp = new Employee("lolo");
grd.DataContext = emp;
}
<Window x:Class="WpfTest.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">
<Grid Name="grd">
<TextBlock Text="{Binding Path=Name}" Name="textBlock1"></TextBlock>
</Grid>
</Window>
your help is very much appreciated
|
|
|
|
|
Could you please supply the code for your Employee class? There's no problem with your XAML or the MainWindow class.
|
|
|
|
|
class Employee
{
string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public Employee()
{ }
public Employee(string strName)
{
this.Name = _name;
}
}
thank you Pete
|
|
|
|
|
Try this:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnChanged(string property)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler == null) return;
handler(this, new PropertyChangedEventArgs(property));
}
}
public class Employee : ViewModelBase
{
string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name == value) return;
_name = value;
OnChanged("Name");
}
}
public Employee(string strName)
{
this.Name = strName;
}
} BTW - your problem was actually in the constructor. You had:
this.Name = _name Effectively you were assigning an empty value to Name because you were using the wrong variable; it should have been strName.
|
|
|
|
|
|
No problem, I'm glad to help.
|
|
|
|
|
Good catch!
The more anger towards the past you carry in your heart, the less capable you are of loving in the present.
My Blog![ ^]
|
|
|
|
|
You need to implement an interface called an INOtifyPropertyChanged interface[^]in your class.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
Strictly speaking, in this case he doesn't because of the sequence of events he's using to hook things up. At the point he assigns Name, the class isn't bound to the DataContext so there's nothing for the PropertyChangedEventHandler to raise the notification to.
The problem was down to an incorrect variable assignment in his code. I've added some code to show him a ViewModelBase implementation so that he gets an understanding, and so that binding will work property for him.
|
|
|
|
|
Pete O'Hanlon wrote: Strictly speaking, in this case he doesn't because of the sequence of events he's using to hook things up. At the point he assigns Name, the class isn't bound to the DataContext so there's nothing for the PropertyChangedEventHandler to raise the notification to.
Point noted and understood.
However, this sort of code is not really going to solve any purpose, as when the property value changes, the UI is not going to be refreshed.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
modified on Wednesday, May 18, 2011 2:43 AM
|
|
|
|
|
Oh, I agree entirely. My first port of call is always a ViewModelBase class of some description.
|
|
|
|
|
I've encountered:
<Grid Name="GrdParams" >
<TextBlock Grid.Column="0" Grid.Row="0" Text="Frequency" VerticalAlignment="Center" />
<Slider Grid.Column="1" Grid.Row="0" Minimum="0" Maximum="0.25" SmallChange="0.001" Value="{Binding Mode=TwoWay, Path=Frequency}" ValueChanged="FreqSlider_ValueChanged" />
<TextBlock Name="TxtFreq" Grid.Column="2" Grid.Row="0" />
</Grid>
and
private void FreqSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (this.TxtFreq != null)
{
this.TxtFreq.Text = (sender as Slider).Value.ToString("###0.####");
}
} Which I found strange there was no "INotifyPropertyChanged" implementation or anything just the binding takes care of it.
Wolves smell fear slay and the coward, men smell gold and slay his brother.
|
|
|
|
|
Hello All,
I have this application that creates a DrawingGroup in a background thread.
How can I render this on a canvas of the main thread after the background thread is completed?
Any attempt results in an exception that the DrawingGroup belongs to different thread.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.
modified on Monday, May 16, 2011 8:53 PM
|
|
|
|
|
Paul Selormey wrote: How can I render this on a canvas of the main thread after the background thread is completed?
Freeze it or create it on the UI thread...only two options I can think of...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the reply.
Trying to avoid creating it on the UI thread since it takes a lot of time.
I will try the freezing and see if it will work.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.
|
|
|
|
|
Hello Mark,
Thanks so much, freezing it works as expected.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.
|
|
|
|
|
Hi,
I'm just wondering how to modify DataTemplate programmatically? Here's my XAML code:
<ListView Grid.Row="2" Name="lstvTCKeyValue" Margin="10,33,10,0" ItemsSource="{Binding}" MouseDoubleClick="lstvTCKeyValue_MouseDoubleClick" KeyDown="lstvTCKeyValue_KeyDown" SelectionChanged="lstvTCKeyValue_SelectionChanged" GridViewColumnHeader.Click="lstvTCKeyValue_Click">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<DataTemplate x:Key="Column1">
<Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=test_config_key}">
<TextBlock.ToolTip>
<TextBlock Text="{Binding Path=test_config_key}"></TextBlock>
</TextBlock.ToolTip>
</TextBlock>
</Border>
</DataTemplate>
I'd like to modify border settings of data template named "Column1".
Thank you in advance.
Goran
|
|
|
|
|
You cannot modify DataTemplates in Silverlight.
The best you can do is keep alternate templates ready and assign them as required.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
Thank you for idea.
I added new alternative data template to XAML file in this way:
<DataTemplate x:Key="Column3">
<Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
<TextBlock Margin="2,1,1,1" Text="{Binding Path=stand_name}"></TextBlock>
</Border>
</DataTemplate>
<DataTemplate x:Key="Column3_Alternative">
<Border BorderThickness="0,0,0,0" BorderBrush="Gray" Margin="-6,0,-6,0">
<TextBlock Margin="2,1,1,1" Text="{Binding Path=stand_name}"></TextBlock>
</Border>
</DataTemplate>
And I change data template when I click button on the form. I've used this the code:
DataTemplate dt = lstvTCKeyValue.FindResource("Column3_Alternative") as DataTemplate;
lstvTCKeyValue.ItemTemplate = dt;
But, it doesn't work.
Any suggestion?
|
|
|
|
|
There is barely any difference between the two templates (except for a margin).
Try changing the color or something.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
|
Thank you.
But what if I don't want to use DataTemplateSelector?
|
|
|
|
|
Is that a joke?
You can use whatever you want.
I only tried to provide another possible solution.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It seems like everybody has neglected to give you the *RIGHT* answer and just continued to support your wrong direction. You should only have a single template and use triggers, VisualStateManager, DependencyProperties, etc. to modify your data template.
The only time having multiple templates is appropriate is if you are making a control that needs to look different in different situations like one look for Aero and one look for classic, etc.
Doesn't seem like your situation applies in this case, so I'd stick to the single template.
|
|
|
|