wpf新手,因此有点挣扎。 在我们进行全面实施之前,我正在组织一个快速演示
我左边有一个树视图Continent
Country
City structure
当用户选择城市时,它应该在右侧的tabcontrol中填充一些文本框
我做了一些工作,但不能使它适用于复合对象。
简而言之,您可以发现我的zaml或代码有什么问题。 为什么不绑定到我的CityDetails.ClubsCount或CityDetails.PubsCount?
我正在构建的内容基于http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
非常感谢任何建议或回复
public class City
{
public City(string cityName)
{
CityName = cityName;
}
public string CityName { get; set; }
public string Population { get; set; }
public string Area { get; set; }
public CityDetails CityDetailsInfo { get; set; }
}
public class CityDetails
{
public CityDetails(int pubsCount,int clubsCount)
{
PubsCount = pubsCount;
ClubsCount = clubsCount;
}
public int ClubsCount { get; set; }
public int PubsCount { get; set; }
}
public class CityViewModel : TreeViewItemViewModel
{
private City _city;
private RelayCommand _testCommand;
public CityViewModel(City city, CountryViewModel countryViewModel):base(countryViewModel,false)
{
_city = city;
}
public string CityName
{
get { return _city.CityName; }
}
public string Area
{
get { return _city.Area; }
}
public string Population
{
get { return _city.Population; }
}
public City City
{
get { return _city; }
set { _city = value; }
}
public CityDetails CityDetailsInfo
{
get { return _city.CityDetailsInfo; }
set { _city.CityDetailsInfo = value; }
}
}
<DockPanel>
<DockPanel LastChildFill="True">
<Label DockPanel.Dock="top" Content="Title " HorizontalAlignment="Center"></Label>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="Status Bar" ></StatusBarItem>
</StatusBar>
<Grid DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TreeView Name="tree" ItemsSource="{Binding Continents}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected,Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:ContinentViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\Continent.png"/>
<TextBlock Text="{Binding ContinentName}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:CountryViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\Country.png"/>
<TextBlock Text="{Binding CountryName}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type ViewModels:CityViewModel}" >
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\City.png"/>
<TextBlock Text="{Binding CityName}"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
<GridSplitter Grid.Row="0" Grid.Column="1" Background="LightGray"
Width="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Grid Grid.Column="2" Margin="5" >
<TabControl>
<TabItem Header="Details" DataContext="{Binding Path=SelectedItem.City, ElementName=tree, Mode=OneWay}">
<StackPanel >
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding CityName}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Area}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Population}"/>
<!-- DONT WORK WHY-->
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding SelectedItem.CityDetailsInfo.ClubsCount}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding SelectedItem.CityDetailsInfo.PubsCount}"/>
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</Grid>
</DockPanel>
</DockPanel>
答案 0 :(得分:0)
看这里(无关的位被省略):
<TabItem DataContext="{Binding Path=SelectedItem.City, ElementName=tree, Mode=OneWay}">
<StackPanel >
<!-- DONT WORK WHY-->
<TextBlock Text="{Binding SelectedItem.CityDetailsInfo.ClubsCount}"/>
<TextBlock Text="{Binding SelectedItem.CityDetailsInfo.PubsCount}" />
</StackPanel>
</TabItem>
TabItem的DataContext是SelectedItem.City:即DataContext是City。 City没有SelectedItem属性。将绑定更改为例如
<TextBlock Text="{Binding CityDetailsInfo.ClubsCount}" />
即。从当前 DataContext开始路径,在本例中为City对象。
作为提示,Visual Studio输出窗口显示绑定错误,这通常可以提供足够的信息来立即诊断这一点,例如:如果你看到'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''