내가 콤보 디스플레이 카테고리 이름 ProductDialogBox 보여줍니다 때 (즉, 컨버터를 통해 카테고리 ID를 int로 바인더 제본됩니다) 원하는WPF 양방향 바인딩 트 리뷰 선택 항목 - 콤보 상자 텍스트 - ContainerViewModel 속성
해결하지만 수동으로 내가하지 않는 업데이트가 나던 TreeView에서 일부 카테고리를 선택하십시오. 이 솔루션은 이제 작업이 다음 ...
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처럼 구현함으로써 = "0"콤보
에 의해 selectedIndex를 추가하는 것입니다
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");
}
}
이전 질문 사이에 양방향 바인딩을 구현하는 방법을 알려주십시오.및 TreeView
선택한 항목을 통해 ComboBox
텍스트가 표시됩니까? TreeView
ComboBox
내부에는 CategoryViewModel
의 컬렉션이 Name
속성으로 표시됩니다. 사용자 TreeView
의 범주, 카테고리 명에 ComboBox
문자 세트 DialogBoxViewModel
속성을 선택했을 CategoryId
선택 CategoryVM
의 Id
으로 설정된다. DialogBoxVM
속성 CategoryId
설정할 때 과 뒷길은 상기 TreeView
선택된 항목을 설정하고 ComboBox
텍스트는 대응하는 카테고리 명을 disaplys된다
부분적 동일한 속성 카테고리 ID에 콤보 선택된 값을 결합함으로써 해결되지만 여전히없는 문제 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>
이렇게 빠른 답장을 보내 주시면 감사하겠습니다. 그러나 SelectedItem의 값이 이미 CategoryViewModel이고 Category ID 만 필요하기 때문에 SelectedCategory 속성을 만들 필요가 없습니다. 이제는 CategoryId가 설정된 경우 ComboBox 텍스트에 범주 Name을 표시하는 것이 가장 중요한 문제입니다. –
아아, 서둘러 질문을 읽어보십시오. 업데이트 된 답변이 도움이되는지 확인하십시오. – Nitin
고맙습니다. 지금 당장 시도해보십시오.그런데 주된 문제가 있다고 생각합니다. CategoryViewModel을 변환기의 ComboBoxItem으로 변환하여 표시 할 수 있습니다. –