我一直试图解决这个问题一天,但仍然没有成功。我正在寻找建议下一步的建议。
问题是,每当我从comobobox中选择一个项目时,ViewModel(VM)中的值都已更改,但是当我触发RelayCommand(在同一个VM中)时,所选项目消失(属性为null)。
当我有用户控件(UC)嵌入另一个用户控件时,事情变得复杂,其中提到了组合框控件。
我的父用户控件的xaml是:
<UserControl
xmlns:lookupPageControl="clr-namespace:LookupPageControl;assembly=LookupPageControl" x:Class="SelectedLookupsControl.SelectedLookups"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignMinWidth="300"
DataContext="{Binding SelectedLookupsPageViewModel, RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Margin="0,0,5,0" Grid.Column="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ListBox Padding="2" SelectionMode="Single" SelectedIndex="0" ItemsSource="{Binding SelectedLookups}" SelectedItem="{Binding SelectedLookupPage}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=LookupViewModel.LookupName}" ToolTip="{Binding Path=LookupViewModel.Description}" ></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
<GridSplitter Grid.Column="0" Background="Gray" Width="5" HorizontalAlignment="Right" VerticalAlignment="Stretch" ResizeBehavior="CurrentAndNext"/>
<DockPanel Grid.Column="1">
<Border DockPanel.Dock="Top">
<TextBlock Margin="5,0,5,0" Text="{Binding Path=SelectedLookupPage.LookupViewModel.LookupName}"/>
</Border>
<Border Padding="2">
<StackPanel HorizontalAlignment="Left">
<lookupPageControl:LookupPage DataContext="{Binding SelectedLookupPage}" />
</StackPanel>
</Border>
</DockPanel>
</Grid>
父母的数据源来自背后的代码(我有理由这样做,不要判断我:)) 视图模型层次结构如此(父VM是根):
+ SelectedLookups : SelectedLookupsViewModel
+-- SelectedLookupPage : LookupPageViewModel
|-- Lookup : LookupViewModel
+-- LookupSchema : LookupSchemaViewModel
+-- LookupAxisLabel : IEnumerable<LookupAxisLabelViewModel>
|-- Label : String
+-- LookupAxis : LookupAxisViewModel
|-- LookupAxisIndex : IList<LookupAxisIndexViewModel>
+-- SelectedLookupAxisIndexViewModel : LookupAxisIndexViewModel
|--- LookupAxisIndexId : int
|--- DisplayName : string
|--- SequenceNumber : int
其中,子级UC属性 DataContext 已用作嵌入式VM的网关(父级包含子级VM)。
My Child用户控件使用 SelectedLookupPage 属性,并将绑定地图数据从VM映射到控件(一切正常,所有数据都显示):
<UserControl x:Class="LookupPageControl.LookupPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:PELookup.ViewModels;assembly=PELookup"
mc:Ignorable="d"
d:DesignMinHeight="100" d:DesignMinWidth="320" >
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Converters.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<DockPanel>
<StackPanel>
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<RadioButton Padding="5,0,5,0" Margin="5,10,0,0" Name="SelectFileRBtn" Grid.Column="0" GroupName="Option1" VerticalAlignment="Center" Content="Select file to import" Grid.Row="1" IsChecked="{Binding SelectedLookupHandlingOption, Converter={StaticResource BoolToLookupPageSelectedTypeEnum}, Mode=TwoWay, ConverterParameter=SelectedFileToImport}" />
<RadioButton Padding="5,0,5,0" Margin="5,10,0,0" Name="OnePropRBtn" Grid.Column="0" GroupName="Option2" VerticalAlignment="Center" Content="Change one property" Grid.Row="5" IsChecked="{Binding SelectedLookupHandlingOption, Converter={StaticResource BoolToLookupPageSelectedTypeEnum}, Mode=TwoWay, ConverterParameter=ChangeOneProperty}" />
<TextBox Grid.Column="0" Margin="15,10,0,0" Grid.Row="2" Width="200" HorizontalAlignment="Left" IsEnabled="{Binding IsEnabledSelectFile}" Name="FilePathTxt" Text="{Binding FilePath, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Column="1" Margin="0,10,5,0" Grid.Row="2" Width="100" HorizontalAlignment="Left" IsEnabled="{Binding IsEnabledSelectFile}" Click="Button_Click" >Open File</Button>
<Button Grid.Column="2" Margin="0,10,5,0" Grid.Row="2" Width="100" HorizontalAlignment="Left" IsEnabled="{Binding IsSendButtonEnabled}" Command="{Binding SendFileCmd}" >Send</Button>
<GroupBox Visibility="{Binding ProgressBarGroupVisibility}" Margin="5,10,5,0" Header="Lookup file upload" Grid.Row="3" Grid.ColumnSpan="3" Grid.Column="0">
<WrapPanel Margin="5" >
<ProgressBar Margin="0,5,0,5" HorizontalAlignment="Stretch" Orientation="Horizontal" Visibility="Visible" Height="10" IsIndeterminate="{Binding IsFileSendProgressBarIsIndeterminate}"
Minimum="0" Maximum="100" Value="{Binding FileSendProgressBarValue}" Width="300" Name="FileSendProgressBar" />
<Image Margin="10,0,0,0" Visibility="{Binding ElementName=FileSendProgressBar, Path=Value, Converter={StaticResource IntToVisibilityConverter}, Mode=OneWay}" Source="{Binding IsFileSentSuccess, Converter={StaticResource BoolToPathFileSuccFailConverter}}" Height="30" />
</WrapPanel>
</GroupBox>
<GroupBox Visibility="{Binding IsFileSentSuccess, Converter={StaticResource BoolToVisibilityCollapsedConverter}}" Margin="5,5,5,0" Header="Lookup processing" Grid.Row="4" Grid.ColumnSpan="3" Grid.Column="0">
<WrapPanel Margin="5" >
<ProgressBar Margin="0,5,0,5" HorizontalAlignment="Stretch" Orientation="Horizontal" Visibility="{Binding IsSendFileProgressBarVisible, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}" Height="10" IsIndeterminate="{Binding IsProgressBarIsIndeterminate}"
Minimum="0" Maximum="100" Value="{Binding ImportedFileProgress}" Width="300" Name="LookupProcessingProgressBar" />
<Image Margin="10,0,0,0" Visibility="{Binding ElementName=LookupProcessingProgressBar, Path=Value, Converter={StaticResource IntToVisibilityConverter}, Mode=OneWay}" Source="Images\success.png" Height="30" />
</WrapPanel>
</GroupBox>
</Grid>
<StackPanel IsEnabled="{Binding IsEnabledChangeOneProp}">
<GroupBox Margin="5,10,5,0" Header="Lookup Axis">
<ItemsControl Margin="5,0,5,0" ItemsSource="{Binding Path=LookupSchema.LookupAxisLabel}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label MinWidth="100" Grid.Column="0" Grid.Row="0" Content="{Binding Label}" VerticalAlignment="Bottom" Padding="5,5,5,0" />
<ComboBox MinWidth="100" Grid.Column="1" Grid.Row="0" Height="Auto" Name="cmbName" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Path=LookupAxis.SelectedLookupAxisIndexViewModel, Mode=TwoWay}"
ItemsSource="{Binding Path=LookupAxis.LookupAxisIndex}" VerticalAlignment="Top" Padding="5,5,5,0">
<TextSearch.TextPath>DisplayName</TextSearch.TextPath>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}">
<!--<Binding Path="SequenceNumber" />-->
<Binding Path="DisplayName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
<Button Margin="5,10,5,0" Width="100" HorizontalAlignment="Right" Command="{Binding GetLookupCellsCmd}" IsEnabled="{Binding IsGetLookupDataBtnEnabled}" >Get lookup data</Button>
<DataGrid Name="CellResultDataGrid" Margin="5,10,5,0" ItemsSource="{Binding Path=LookupCellsResultData}" Visibility="{Binding Path=LookupCellSearchViewModel, Converter={StaticResource NullGridToGridVisibility}}" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
CanUserSortColumns="True" CanUserAddRows="False" AutoGeneratingColumn="CellResultDataGrid_AutoGeneratingColumn" />
<Button Margin="5,10,5,5" Width="100" HorizontalAlignment="Right" Command="{Binding SaveLookupCellsCmd}" IsEnabled="{Binding IsSaveCellBtnEnabled}" Visibility="{Binding ElementName=CellResultDataGrid, Path=Visibility}" >Save</Button>
</StackPanel>
</StackPanel>
</DockPanel>
不幸的是,当我调用RelayCommand obj来更新另一个我需要知道项目用户选择的数据的时候,当selectedItem值被更改(VM中的值也被更改)时, value为null
让我想知道,如果嵌入式UC的DataSource属性没有权限拒绝更新作为父VM(嵌入式)的一部分的VM。然后我尝试从后面的代码中加载组件,其中整个父VM被注入到子UC中,我最终得到了相同的行为。 出于某些原因,看起来像组合框控制让我的生活更加艰难。
请有人帮助我。
FYI我的方式&#34; LookupSchema&#34;填充的属性:
public LookupSchemaViewModel LookupSchema
{
get
{
if (_lookupSchema == null)
{
Task.Run(() => UpdateLookupSchema());
}
return _lookupSchema;
}
set
{
SetProperty(ref _lookupSchema, value);
}
}
当你点击按钮&#34;获取查找数据&#34;时,另一个命令被用来触发调用&#34; GetLookupCells&#34;方法如下:
private void GetLookupCells(object obj)
{
try
{
lock (Lock)
IsInProgress = true;
lock (LookupSchema)
{
var x =
LookupSchema.LookupAxisLabel.FirstOrDefault(
axi => axi.LookupAxis.AxiSequenceIndex == (int)LookupAxiSequenceIndexEnum.X);
var y =
LookupSchema.LookupAxisLabel.FirstOrDefault(
axi => axi.LookupAxis.AxiSequenceIndex == (int)LookupAxiSequenceIndexEnum.Y);
var z =
LookupSchema.LookupAxisLabel.FirstOrDefault(
axi => axi.LookupAxis.AxiSequenceIndex == (int)LookupAxiSequenceIndexEnum.Z);
LookupCellSearchViewModel = _lookupRestAPIService.GetCellSearch(LookupViewModel.LookupId,
x != null ? x.LookupAxis.SelectedLookupAxisIndexViewModel.DisplayName : String.Empty,
y != null ? y.LookupAxis.SelectedLookupAxisIndexViewModel.DisplayName : String.Empty,
z != null ? z.LookupAxis.SelectedLookupAxisIndexViewModel.DisplayName : String.Empty)
.ToLookupCellSearchViewMolel();
// subscribe on value change to update "save cell value button"
LookupCellSearchViewModel.LookupCells.ToList().ForEach(item=>item.ToList().ForEach(vm=>vm.PropertyChanged +=
(sender, args) => RaisePropertyChanged(() => IsSaveCellBtnEnabled)));
}
}
catch (Exception ex)
{
lock (ErrorMessage)
ErrorMessage = ex.Message;
}
finally
{
lock (Lock)
IsInProgress = false;
}
}
答案 0 :(得分:0)
通常回答这类问题并不容易,但请尝试检查这些项目并检查其提到的更改:
ViewModel
和Test Axis Combobox
以及其他Get lookup data Button
和主持人{{1}时,请确保您引用UserControls
的一个实例。 }}。Window
属性中UpdateLookupSchema()
使用LookupSchemaViewModel
Task.Run
文件中的每个已使用属性都属于以下属性之一:
Xaml
部分中调用ViewModel
的{{1}}的媒体资源。查看this article,它会在OnPropertyChanged
类中正确实现Set
接口。INotifyPropertyChanged
dependency property
VM
属性和UserControl
命令中的用户控件中添加一些括号,以检查单击LookupSchema
按钮时的确切情况。 我们在您的问题中没有您的GetLookupCellsCmd
课程和其他部分项目,所以当您点击get lookup data
时,我们不知道其中发生了什么!
答案 1 :(得分:0)
好的,在玩了我的虚拟机后,我发现我的“LookupAxisLabel”集合不是可观察集合的类型。
这一改变之后一切正常。
感谢大家的评论,感谢它。