我對 WPF 真的很陌生,并且剛剛開始關注 MVVM/Expression.Blend。
我正在嘗試將組合框“SelectedIndexChanged”事件與 InvokeCommandAction 結合使用,系結到 ViewModel 中指定的命令。
但是,命令中的代碼永遠不會執行(值myvalue永遠不會更新)。
我在這里做錯了什么?
XAML:
<UserControl x:Class="GDX.UI.Views.AnalyseView"
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:i1="http://schemas.microsoft.com/xaml/behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:GDX.UI.Views"
mc:Ignorable="d"
Height="720" Width="1210">
<Control.Resources>
<ResourceDictionary Source="pack://application:,,,/GDX.UI;component/ResourceDictionary/ResourceDictionary.xaml"/>
</Control.Resources>
<materialDesign:Card Margin="15,15,15,15" Grid.Column ="0" Grid.Row="0" Grid.RowSpan="6" Grid.ColumnSpan="2">
<ScrollViewer Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Hidden">
<ItemsControl ItemsSource="{Binding buildingComponents}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="10">
<TextBlock Margin="10" Text="{Binding Path=UIName}" FontWeight="Bold"/>
<ComboBox
ItemsSource="{Binding Path=ConstructionOptions}"
SelectedItem="{Binding Path=Construction}"
HorizontalAlignment="Center"
Style="{StaticResource MaterialDesignOutlinedComboBox}"
Width="256"
Height="50"
BorderBrush="Red"
materialDesign:HintAssist.Hint="Baukonstruktion">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedIndexChanged">
<i:InvokeCommandAction Command="{Binding SetBuildingComponentConstruction}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</materialDesign:Card>
視圖模型:
using GDX.IO.Schemas;
using GDX.IO.Statics;
using GDX.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace GDX.ViewModel.ViewModels
{
public class AnalyseViewModel : BaseViewModel
{
public ObservableCollection<BuildingComponent> buildingComponents { get; set; }
public double myvalue { get; set; }
public ICommand SetBuildingComponentConstruction { get; set; }
public AnalyseViewModel()
{
this.buildingComponents = new ObservableCollection<BuildingComponent>();
if (Building.BuildingComponents.Count != 0) //if contains elements
{
foreach (BuildingComponent bcomp in Building.BuildingComponents)
{
this.buildingComponents.Add(bcomp);
}
}
this.SetBuildingComponentConstruction = new RelayCommand(this.setBuildingComponentConstruction);
this.myvalue = 0;
}
public void setBuildingComponentConstruction()
{
myvalue = 1;
}
}
}
uj5u.com熱心網友回復:
看起來 SetBuildingComponentConstruction 方法是在 AnalyseViewModel 上定義的,但是您在專案模板中系結的資料背景關系將是 BuildingComponent。您可能必須“走”到您的 UserControl 的資料背景關系。
如何將 WPF 系結與 RelativeSource 結合使用?
在除錯時查看 VS 中的 XAML Binding Failures 選項卡也可以為您提供一些方向。
嘗試
Command="{Binding DataContext.SetBuildingComponentConstruction,
RelativeSource={RelativeSource AncestorType=ComboBox}}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521345.html
標籤:C#wpf虚拟机数据绑定
