2012-03-15 3 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 코드는 이제 "0"우선 순위 컬럼에 대해 항상있다

 <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> 

입니다. 다른 바인딩 변수는 올바르게 표시되지만 문자열입니다.

+1

작업을 어떻게 작성합니까? 우선 순위가 0이 아니겠습니까? 코드가 정상적으로 보이므로 작동해야합니다. – nemesv

+0

도 ctor에서 초기화 한 후 우선 순위 값이 변경됩니까? –

+0

'list.Add (새 작업 ("test", "testowe zadanie", 1)); ' – deem

답변

1

일반적으로 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

당신은 어떤 일이 잘못하지 않았다, 그러나 우선 순위가의 단서이고 다른 일부에 덮어 쓰기 때문에 코드를 확인하여 다른 바인딩은 제대로 작동합니다. ControlProperty = "{Binding ClassProperty, UpdateSourceTrigger = PropertyChanged}"와 같은 모든 속성에서 바인딩을 변경하는 것을 잊지 마십시오.

+0

이 작업을 수행하려면 INotifyPropertyChanged를 구현하고 속성에서 OnPropertyChanged 이벤트를 발생시켜야합니다. –

관련 문제