我無法將整數變量綁定到模板。如何將整數綁定到WPF模板
我的C#代碼如下所示:
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;
}
}
和XAML代碼
<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>
總是有 「0」 的優先級列了。其他綁定變量顯示正確,但它們是字符串。
你如何創建你的任務?你確定'Priority'不是0嗎?因爲你的代碼看起來不錯,它應該可以工作 – nemesv 2012-03-15 19:19:43
也是,在ctor初始化後改變優先值? – 2012-03-15 19:22:23
'list.Add(new Task(「test」,「testowe zadanie」,1));' – deem 2012-03-15 19:39:40