2017-01-30 60 views
0

的DataTemplate控制我新的WPF中,我創建了一個列表框,將創建一個動態listItems中,在這裏,我用datetemplate其中包含兩個控件是二的TextBlocks,一個含有的TextBlocks綁定值形成組合框(它是字符串數據類型),另一個是綁定代碼綁定的值。從綁定代碼背後的價值是在WPF

XAML

<ListBox ItemsSource="{Binding obj}" HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="201" BorderBrush="#555555" Margin="80,40,0,0" VerticalAlignment="Top" Width="282" Background="#555555" > 
    <ListBox.ItemTemplate> 
      <DataTemplate> 
      <StackPanel Orientation="Horizontal" Margin="5" > 
       <TextBlock Height="40px" Width="80px" Text="{Binding roundedhourvalue, FontSize="24" Background="#555555" Foreground="White"></TextBlock> 
       <TextBlock x:Name="items" Text="{Binding}" Margin="35,0,0,0"></TextBlock> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

C#(Roundedhour.cs)

public class Roundedhour 
{ 
    public string hourvalue { get; set; } 

    public override string ToString() 
    { 
     return string.Format("{0}", hourvalue); 
    } 
} 

在這個類創建用於hourvalue的性質。對於這個類,我在下面提到的代碼隱藏文件中創建了一個對象。創建一個對象併爲hourvalue變量賦值。

C#(代碼後面)

{ 
    if (dispatcherTimer1.Interval == TimeSpan.FromSeconds(15)) 
    { 
     //lstbxindex.Items.Add(lstbxindex.SelectedItem.ToString()); 

     string hrvalue = Convert.ToString(hrvalueinitially); 

     obj = new Roundedhour(); 
     obj.hourvalue = Convert.ToString(hrvalueinitially); 

     string roundedhourvalue =obj.hourvalue; 

     this.DataContext = this; 


     //lblprojectAhr.Content = string.Join(",", hrvalueinitially + "" + "hr"); 
    } 
} 

在這裏,我創建Rounderhour class.Assign值的對象來表示屬性小時值。但我無法將codebind的值綁定到XAML頁面。

+0

你的'DataContext'需要一個'public'屬性包含'{get; }(getter)'從你的'Binding'中獲取值。還要注意'Roundedhour'中的屬性叫做'hourvalue'而不是'roundhourvalue'。 **當發生綁定問題時,請務必在VisualStudio中檢查'輸出'窗口。** - 在那裏您將看到缺少的地方... –

回答

-1

分配XAML對象「的ItemsSource」屬性與你綁定變量。

而且這是完全錯誤的結合對象本身成爲對象的屬性一樣

this.DataTemplate = this; 

用途:

List<yourobject> bindingObjectList = new List<yourobject>(); 
// insert your objects into the list 
this.ItemsSource = bindingObjectList; 

在這裏你可以找到一個例子:

Grid & Pivot Binding Example for multiple DataTemplates

+0

this.ItemsSource = obj;獲取在this.ItemsSource中的值,但沒有綁定到標籤的值 – user688

+0

您使用「Listbox」並且它具有必須在代碼隱藏中分配的ItemsSource屬性,並且這無法在代碼示例中完成:this.DataContext = this;你需要分配itemssource一個集合像(列表,Ienumerable等)。所以你對任何東西都沒有任何約束力。 –

1

ItemsSource應是一個CollectionType

ListBox ItemsSource="{Binding obj}" 

你也應該開始給你的變量和屬性有意義的名字。這使得後面更容易閱讀你的代碼o n。

第二個問題是在你的Binding本身。

您結合這樣的:Text="{Binding roundedhourvalue}

所以WPF是在obj期待財產roundedhourvalue

但是,如您的CodeBehind所示,只有obj.hourvalue

因此改變你的綁定到Text="{Binding hourvalue}

檢查Output-Window瞭解詳情。

注:

string roundedhourvalue = obj.hourvalue; 

沒有getter和自private不accsessible。

注意:你要麼使用Binding您集的ItemsSource的代碼隱藏。


試試這樣說:

只是刪除所有的格式和東西:

<ListBox ItemsSource="{Binding RoundedHours}" x:Name="ListBox"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal" Margin="5" > 
       <TextBlock Text="{Binding hourvalue}"></TextBlock>     
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

而隱藏代碼更改此:

private void UpdateDataContext(object hrvalueinitially) 
    { 
     List<Roundedhour> hours = new List<Roundedhour>(); 
     hours.Add(new Roundedhour() 
     { 
      hourvalue = hrvalueinitially.ToString() 
     }); 

     //Set the ItemsSource in code: => remove your ItemsSource Binding from XAML 
     listBox.ItemsSource = hours; 
    } 

或者你可以使用'MVVM'方法:

public class MyViewModel : INotifyPropertyChanged 
    { 
     //IMPLEMENT INotifyPropertyChanged HERE PLS 

     public ObservableCollection<RoundedHour> Collection { get; set; } = new ObservableCollection<RoundedHour>(); 

     private void AddToCollection(object hrvalueinitially) 
     { 
      Collection.Add(new RoundedHour() 
      { 
       hourvalue = hrvalueinitially.ToString() 
      }); 
      OnPropertyChanged("Collection"); 
     } 

     //Make sure to set your Windows DataContext to an Instance of this Class 
    } 
+0

我改變了Text = {Binding hourvalue},但沒有綁定到標籤中的值。 – user688

+0

您應該閱讀有關 [MVVM](https://msdn.microsoft.com/en-us/library/hh848246.aspx) –

+0

System.Windows.Data錯誤:40:BindingExpression路徑錯誤:未找到'obj'屬性'對象'''字符串'(HashCode = -2067273858)'。 BindingExpression:路徑= OBJ; DataItem ='String'(HashCode = -2067273858);目標元素是'標籤'(Name ='');目標屬性是'Content'(輸入'Object') – user688