gpt4 book ai didi

wpf - 在 Josh Smith 的文章之后,在 MVVM 的 ListView 中添加 ComboBox

转载 作者:行者123 更新时间:2023-12-03 10:18:22 27 4
gpt4 key购买 nike

我最近开始使用 MVVM 开发 WPF 应用程序。我关注 Josh Smith's article .我编写了一个基本屏幕,它只是在 ViewModel 的 ObservableCollection 类的 ListView 中显示数据。这是一个 List<string> .我只是希望能够显示字符串列表,然后能够将其保存回父集合的基础属性中。
我应该使用RelativeSource 作为DataContext 与ViewModel 链接吗?
这是 ViewModel 代码。

public class ViewModel
{
public List<string> AndOrList
{
get
{
List<string> andOrList = new List<string> { "AND", "OR" };
return andOrList;
}
}

public ObservableCollection<BusObjects.Criterion> Criteria
{
get
{
return new ObservableCollection<BusObjects.Criterion>(_stream.ParseFilterCriteria().Criterias);
}
}
}
这是问题区域
<GridViewColumn Header="And / Or">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding AndOrList}">
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这是整个 XAML 代码。我正在尝试将 AndOrList 集合绑定(bind)到 ComboBox。
<UserControl.Resources>
<local:FilterCriteriaConverter x:Key="FilterCriteriaConverter"/>

<CollectionViewSource x:Key="CriteriaList"
Source="{Binding Path=Criteria}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="DimName" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<Style x:Key="CriteriaItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ItemsControl.AlternationIndex" Value="1" />
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="#EEEEEEEE" />
</MultiTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>

<DockPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListView
Name="lstCriteria"
Grid.Row="0"
AlternationCount="2"
DataContext="{StaticResource CriteriaList}"
ItemContainerStyle="{StaticResource CriteriaItemStyle}"
ItemsSource="{Binding}" DockPanel.Dock="Top">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Rectangle Fill="SteelBlue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="And / Or">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding AndOrList}">
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=DimName}" />
<GridViewColumn Header="Oprator" DisplayMemberBinding="{Binding Path=Operator}" />
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=DimValue}" />

</GridView>
</ListView.View>
</ListView>
<Border Grid.Row="1" CornerRadius="4" BorderThickness="1">
<GridSplitter HorizontalAlignment="Stretch" VerticalAlignment="Center"
Height="3"
Background="SteelBlue"/>
</Border>
<Border Name="xmlBorder" Grid.Row="2" VerticalAlignment="Stretch" CornerRadius="4" BorderThickness="1" >
<StackPanel x:Name="stkPanel" Grid.Row="2" Height="Auto" VerticalAlignment="Stretch" >
<XMLViewer:Viewer x:Name="xmlViewer" VerticalAlignment="Stretch" />
<TextBox x:Name="txtXml" TextChanged="XmlTextChanged" Visibility="Hidden"
Text="{Binding Path=FilterCriteria,
Converter={StaticResource FilterCriteriaConverter}}" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</Grid>
</DockPanel>
谢谢

最佳答案

我会去DataContext仍然是 View 模型,将路径更改为 DataContext.AndOrList并添加 RelativeSource找到相应的控件。

此外,我不会更改 ListView 的 DataContext,只需将所有内容都放在 ItemsSource 绑定(bind)中:

<ListView ItemsSource="{Binding Source={StaticResource CriteriaList}}" ...>

然后以下应该工作并且遍历是最小的:
<ComboBox ItemsSource="{Binding DataContext.AndOrList, RelativeSource={RelativeSource AncestorType=ListView}}">

关于wpf - 在 Josh Smith 的文章之后,在 MVVM 的 ListView 中添加 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6864742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com