Reputation: 1884
I can't bind an integer variable to the template.
My C# code looks like below:
class Task
{
public string name;
public string desc;
public int pr;
public string TaskName
{
get { return name; }
set { name = value; }
}
public string Description
{
get { return desc; }
set { desc = value; }
}
public int Priority
{
get { return pr; }
set { pr = value; }
}
public Task(string name, string description, int pr)
{
this.TaskName = name;
this.Description = description;
this.Priority = pr;
}
}
and the XAML code is
<DataTemplate x:Key="myTaskTemplate">
<Border Name="border" BorderBrush="DarkSlateBlue" BorderThickness="2"
CornerRadius="2" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Padding="0,0,5,0" Text="Task Name:"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=TaskName}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Padding="0,0,5,0" Text="Description:"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Padding="0,0,5,0" Text="Priority:"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Priority}"/>
</Grid>
</Border>
</DataTemplate>
There is always "0" for the Priority column now. The other binding variables are displayed correctly, but they are strings.
Upvotes: 0
Views: 743
Reputation: 4012
you didn't do any thing wrong, But check your code because the priority is overwritten in some where else the clue of that is your other binding works fine, don't forget to change your binding in all the property to be like ControlProperty="{Binding ClassProperty,UpdateSourceTrigger=PropertyChanged}"
Upvotes: 1
Reputation: 8039
Ussualy the ViewModel should implement INotifyPropertyChanged in order to propagate changes in properties to the view.
This being said, your class should look like this:
class Task : INotifyPropertyChanged
{
public string name;
public string desc;
public int pr;
public string TaskName
{
get { return name; }
set
{
name = value;
OnPropertyChanged("TaskName");
}
}
public string Description
{
get { return desc; }
set
{
desc = value;
OnPropertyChanged("Description");
}
}
public int Priority
{
get { return pr; }
set
{
pr = value;
OnPropertyChanged("Priority");
}
}
public Task(string name, string description, int pr)
{
this.TaskName = name;
this.Description = description;
this.Priority = pr;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string pName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(pName));
}
}
}
Upvotes: 1