解决了
当 ProductDialogBox 显示时,我希望 ComboBox 显示类别名称(通过转换器绑定到 int CategoryId),但除非我从 TreeView 手动选择某个类别,否则它不会更新。解决方案是将 SelectedIndex="0" 添加到 ComboBox
到目前为止,任务的实现如下...
ProductDialogBox.xaml
<UserControl.Resources>
<converter:CategoryIdConverter x:Key="categoryIdConverter" />
<HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Path=Categories}">
<StackPanel Orientation="Horizontal">
<Image Source="/GSM.UI;component/Resources/Images/folder.png" Margin="1,1,3,1"/>
<TextBlock x:Name="tb" Text="{Binding Path=Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate x:Key="CategoryItemTemplateCollapsed">
<TextBlock Text="{Binding Path=Name, Mode=OneWay}"
DataContext="{Binding
Path=DataContext.CategoryId,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
Converter={StaticResource categoryIdConverter}}"/>
</DataTemplate>
<DataTemplate x:Key="CategoryItemTemplateExpanded">
<TreeView ItemTemplate="{StaticResource NodeTemplate}" HorizontalAlignment="Stretch" Margin="-4 0 -4 0" Foreground="#FF3C3C3C"
ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}">
<i:Interaction.Behaviors>
<b:BindableSelectedItemBehavior SelectedItem_="{Binding
Path=DataContext.CategoryId, Mode=TwoWay,
Converter={StaticResource categoryIdConverter},
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
</i:Interaction.Behaviors>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</DataTemplate>
</UserControl.Resources>
<ComboBox ItemsSource="{Binding Path=Categories}" SelectedIndex="0">
<ComboBox.ItemTemplateSelector>
<dt:CategoryTemplateSelector/>
</ComboBox.ItemTemplateSelector>
</ComboBox>
</UserControl>
ProductDialogViewModel.cs
public ProductDialogViewModel(ProductViewModel product, bool update)
{
if(update)
_product = updateProduct.Product;
else
_product = new Product();
}
public int CategoryId
{
get { return _product.CategoryId; }
set
{
if (value.Equals(_product.CategoryId))
return;
_product.CategoryId = value;
base.OnPropertyChanged("CategoryId");
}
}
OLD QUESTION
请告知我如何通过显示的文本实现DialogBoxViewModel
与TreeView
所选项目之间的两种方式绑定?内部显示按属性ComboBox
的集合。当用户在其中选择一个类别时,一个文本设置为类别名称和属性设置为 selected 。和回退,当属性设置时,选定的项目被设置,文本显示相应的类别名称TreeView
ComboBox
CategoryViewModel
Name
TreeView
ComboBox
DialogBoxViewModel
CategoryId
Id
CategoryVM
DialogBoxVM
CategoryId
TreeView
ComboBox
通过将 ComboBox 选择的值绑定到相同的属性 CategoryId 部分解决了该问题,但在设置 CategoryId 时它仍然不显示任何内容。
// ViewModel
ProductDialogBoxViewModel.cs
int CategoryId
ObservableCollection<CategoryViewModel> Categories;
// View
ProductDialogBoxView.xaml
<ComboBox SelectedValue="{Binding Path=CategoryId, Converter={StaticResource categoryIdNameConverter}, Mode=OneWay}"
DisplayMemberPath="SelectedValue.Name">
<ComboBoxItem>
<TreeView ItemsSource={Binding Categories} ItemTemplate={<-- displays name of category -->}>
<interaction:Interaction.Behaviors>
<behavior:BindableSelectedItemBehavior
SelectedItem_="{Binding Path=CategoryId,
Mode=TwoWay,
Converter={StaticResource categoryNameIdConverter}}" />
</interaction:Interaction.Behaviors>
</TreeView>
<ComboBoxItem>
</ComboBox>