2012-03-15 1 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

どのようにタスクを作成しますか? 'Priority'が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に設定する必要があります。 –

1

あなたは任意のものが間違ってませんでしたが、優先順位は、その手がかりがどこにある他のいくつかに上書きされるので、あなたのコードをチェックし、あなたの他のバインディングは正常に動作しますが、ControlProperty = "{Binding ClassProperty、UpdateSourceTrigger = PropertyChanged}"のようにすべてのプロパティでバインディングを変更することを忘れないでください。

+0

これを行うには、INotifyPropertyChangedを実装してプロパティのOnPropertyChangedイベントを発生させる必要もあります –

関連する問題