wpf新手,因此掙扎了一下。 我是一個快速演示放在一起,我們去爲全面實施 之前,我對剩下的樹視圖綁定wpf無法綁定類中的嵌套屬性
Continent
Country
City structure
當用戶選擇城市應該填充一些文本框在一個TabControl右手邊一個TreeView
我做了一些工作,但不能使它與複合對象一起工作。
簡而言之,你可以發現我的zaml或代碼有什麼問題。 爲什麼不綁定到我的CityDetails.ClubsCount或CityDetails.PubsCount?
什麼,我是基於http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
由於建設很多關於任何建議或回覆
的DataModel
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; }
}
}
XAML
這裏10<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>
它的工作原理!!!!! 對不起,如果我發佈無關緊要的東西只是不知道我去了哪裏。 – user9969 2010-03-21 08:33:49