|
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.
|
|
|
|
|
Actually, I've tried the code with setting list view's ItemTemplate instead of setting columns's CellTemplate what I needed.
Therefore, I applied the the method with 2 data templates in list view's resource:
<DataTemplate x:Key="Column3_ShowStandColumn">
<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_HideStandColumn">
<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 this code in combo box's SelectionChanged event:
if (selectedStand.stand_name != "Все")
{
...
DataTemplate dt = lstvTCKeyValue.FindResource("Column3_HideStandColumn") as DataTemplate;
gv.Columns[2].CellTemplate = dt;
...
}
else
{
...
DataTemplate dt = lstvTCKeyValue.FindResource("Column3_ShowStandColumn") as DataTemplate;
gv.Columns[2].CellTemplate = dt;
...
}
Thank you all for suggestions and help.
|
|
|
|
|
Indeed, and for those who are interested, here's an example of a template that can be applied to a checkbox. Basically, this DataTemplate uses a trigger to change a control template when the IsSelected value changes. Just for clarity - this isn't the best way to restyle a checkbox, it just demonstrates using a DataTemplate and ControlTemplate elements. BTW - this is a purely XAML based alternative to a DataTemplateSelector.
<ControlTemplate x:Key="OnTemplate">
<Grid ToolTip="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}">
<Rectangle HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Width="80" Height="20" RadiusX="3" RadiusY="3">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF6183DC" Offset="0"/>
<GradientStop Color="#FFC6CDE0" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="ON" TextWrapping="Wrap" Margin="8,0,0,0" FontSize="16" FontWeight="Bold" Foreground="#FFFEFCFC"/>
<Rectangle RadiusX="3" RadiusY="3" HorizontalAlignment="Left" Margin="40,0,0,0" VerticalAlignment="Center" Width="40" Height="20">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFB5B1B1" Offset="0.004"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="OffTemplate">
<Grid ToolTip="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}">
<Rectangle HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Width="80" Height="20" RadiusX="3" RadiusY="3">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE0E7F8" Offset="0"/>
<GradientStop Color="#FFF2F4FA" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="OFF" TextWrapping="Wrap" Margin="44,0,0,0" FontSize="16" FontWeight="Bold" Foreground="#FFB3ABAB"/>
<Rectangle RadiusX="3" RadiusY="3" HorizontalAlignment="Left" Margin="1,0,0,0" VerticalAlignment="Center" Width="40" Height="20">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFB5B1B1" Offset="0.004"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
<DataTemplate x:Key="toggleTemplate">
<Control x:Name="toggleControl" Template="{StaticResource OnTemplate}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Value="False">
<Setter Property="Template" TargetName="toggleControl" Value="{StaticResource OffTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
|
|
|
|
|
Hello again!
It is another time I encounter this error: "objects added to an IDictionary must have a Key attribute or some other type of key associated with them.". I searched google to find out what is going on, but in every case the reason was that somebdy didn't set either Key or TargetType.
I my case, I set the TargetType, but it doesn't work. In my previous post the problem was that I was using a UserControl. Now I try to set a template of a ToggleButton which is not a UserControl. WTH is going on?
<ControlTemplate TargetType="ToggleButton">
<ControlTemplate.Triggers>
<DataTrigger Binding="{TemplateBinding IsChecked}" Value="True">
<Setter Property="Opacity" Value="1" />
</DataTrigger>
<DataTrigger Binding="{TemplateBinding IsMouseOver}" Value="True">
<Setter Property="Opacity" Value="0.7" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
(...)
<ToggleButton Grid.Row="3" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" Opacity="0.3">
<Image Source="{StaticResource CheckedImage}" Height="16" />
</ToggleButton>
The error started to appear since I changed something. I had tried to revert my recent changes but I couln't work out what is wrong
EDIT: I've tried to set x:Key and reference Template={StRes key} and got exception:
{"'Set property 'System.Windows.DataTrigger.Binding' threw an exception.' Line number '59' and line position '18'."}
Inner:
{"Unable to cast object of type 'System.Windows.TemplateBindingExpression' to type 'System.Windows.Data.BindingBase'."} Inner:null
Edit2: IsChecked IS a dep property
Any ideas?
Greetings - Jacek
modified on Monday, May 16, 2011 5:46 AM
|
|
|
|
|