我有一本字典。
public Dictionary<string,string> dict = new Dictionary<string,string>();
我将键/值设置为:
dict["Error"], dict["StartTime"], dict["ConnectedTime"] and dict["DialingResult"]
同样在MainWindow.xaml.cs
ObservableCollection<Calls> items = new ObservableCollection<Calls>();
关于Calls
课程,
public class Calls : NotifyUIBase // NotifyUIBase: INotifyPropertyChanged
{
public string dialResult;
public string DialResult {
get { return dialResult; }
set
{
dialResult = value;
RaisePropertyChanged();
}
}
public string dialError;
public string DialError
{
get { return dialError; }
set
{
dialError = value;
RaisePropertyChanged();
}
}
public string startTime;
public string StartTime
{
get { return startTime; }
set
{
startTime = value;
RaisePropertyChanged();
}
}
public string connectedTime;
public string ConnectedTime
{
get { return connectedTime; }
set
{
connectedTime = value;
RaisePropertyChanged();
}
}
public string dialingResult;
public string DialingResult
{
get { return dialingResult; }
set
{
dialingResult = value;
RaisePropertyChanged();
}
}
}
现在我想在窗口中显示每个值。我认为主要代码可能是
<DockPanel>
<ItemsControl Height="300" Name="icTodoList" ItemsSource="{Binding Calls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Value, Mode=OneWay, FallbackValue=' '}" Grid.Column="0"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl></DockPanel>
在MainWindow中:
public MainWindow()
{
InitializeComponent();
icTodoList.ItemsSource = items;
要更新控件,我们有
Dictionary<string,string> dict = new BackgroundProcess(args).RunScript();
Calls c = new Calls();
// if no error:
c.startTime = dict["StartTime"];
c.connectedTime = dict["ConnectedTime"];
c.DialResult = dict["DialingResult"];
Dispatcher.Invoke(() =>
{
items.Add(c);
});
在我的代码中我有
Binding Path=Value
也许是错的。我不知道如何正确绑定值。
答案 0 :(得分:1)
看起来你正在设置ItemsSource两次。
来到这里:
<ItemsControl Height="300" Name="icTodoList" ItemsSource="{Binding Calls}">
另一次在这里:
icTodoList.ItemsSource = items;
此外,您在技术上并未绑定到“字典”,因为您只是在字典中获取值并将它们放入ObservableCollection
。
此外,您要显示Calls类中的哪种类型的信息?如果你想显示ObservableCollection中的每个属性,你可以这样做(通过代码隐藏)就像这样:
<DockPanel>
<ItemsControl Height="300" Name="icTodoList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding DialResult, FallbackValue=' '}" Grid.Column="0"/>
<TextBlock Text="{Binding DialError, FallbackValue=' '}" Grid.Column="1"/>
<TextBlock Text="{Binding StartTime, FallbackValue=' '}" Grid.Column="2"/>
<TextBlock Text="{Binding ConnectedTime, FallbackValue=' '}" Grid.Column="3"/>
<TextBlock Text="{Binding DialingResult, FallbackValue=' '}" Grid.Column="4"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
ItemsControl正在做的是通过代码隐藏绑定到ObservableCollection。然后,Calls类中的每个属性都绑定到TextBlock,因为每个项的范围都是Calls类型。
旁注:您可能希望调查MVVM来执行此操作,而您通常不希望通过代码隐藏来执行此操作。