2012-03-15 50 views
0

我無法將整數變量綁定到模板。如何將整數綁定到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」 的優先級列了。其他綁定變量顯示正確,但它們是字符串。

+1

你如何創建你的任務?你確定'Priority'不是0嗎?因爲你的代碼看起來不錯,它應該可以工作 – nemesv 2012-03-15 19:19:43

+0

也是,在ctor初始化後改變優先值? – 2012-03-15 19:22:23

+0

'list.Add(new Task(「test」,「testowe zadanie」,1));' – deem 2012-03-15 19:39:40

回答

1

Ussualy ViewModel應實現INotifyPropertyChanged以便將屬性更改傳播到視圖。

這是說,你的類應該是這樣的:

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)); 
     } 
    } 
} 
+0

他可能真的需要這樣做,但這不是真正的問題。他在構造函數中設置屬性,以便在視圖從屬性獲取值之前將其設置爲1。 – 2012-03-19 05:48:57

1

你沒有做任何事情錯了,但檢查你的代碼,因爲其優先覆蓋的一些其他地方的的線索就是你的其他綁定工作正常,不要忘記改變你的所有屬性的綁定是像ControlProperty =「{Binding ClassProperty,UpdateSourceTrigger = PropertyChanged}」

+0

如果他想這樣做,他還需要實現INotifyPropertyChanged並在屬性中觸發OnPropertyChanged事件 – 2012-03-19 05:47:46