binting中的bindings中的项目如何在不使用Messenger或EventAggregator的情况下相互通信。以下是一个简化但完整的情况。我在ItemScontrol中有两个相关的项目。可以通过在Prism中使用EventAggregator(Messenger(来完成UpdateTheSecondemfromthefirstitem((。但是我想知道是否还有其他直接解决方案可以在不使用Messenger的情况下实现这一目标。MainWindow:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="MyType">
<Grid>
<Slider Value="{Binding MyNum}" Minimum="0" Maximum="100"></Slider>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
mytype
using Prism.Mvvm;
namespace WpfApp2
{
public class MyType : BindableBase
{
private bool _isFirstItem=false;
private int _myVar;
public int MyVar
{
get { return _myVar; }
set
{
if (Equals(_myVar, value)) return;
SetProperty(ref _myVar, value);
}
}
public MyType(bool isFirst)
{
_isFirstItem = isFirst;
MyVar = 0;
}
}
}
mainWindowViewModel
using Prism.Mvvm;
using System.Collections.ObjectModel;
namespace WpfApp2
{
public class MainWindowViewModel:BindableBase
{
private ObservableCollection<MyType> _myItems = new ObservableCollection<MyType>();
public ObservableCollection<MyType> MyItems
{
get { return _myItems; }
set
{
if (Equals(_myItems, value)) return;
SetProperty(ref _myItems, value);
if(_isFirstItem) UpdateTheSecondItemFromTheFirstItem();
}
}
private void UpdateTheSecondItemFromTheFirstItem(){
}
public MainWindowViewModel()
{
MyType[] MyTypeArr =
{
new MyType(true),
new MyType(false)
};
MyItems.AddRange(MyTypeArr);
}
}
}
找到解决方案,只需使用ref,就像C 中的指针:
mainWindowViewModel:
public MainWindowViewModel()
{
MyType Type1 = new MyType();
MyType Type2 = new MyType();
MyItems.Add(Type1);
MyItems.Add(Type2);
Type1.SetPair(ref Type2);
}
mytype.cs:
using Prism.Mvvm;
namespace WpfApp2
{
public class MyType : BindableBase
{
private int _myVar;
public int MyVar
{
get { return _myVar; }
set
{
if (Equals(_myVar, value)) return;
SetProperty(ref _myVar, value);
if (MyPair!=null)
{
MyPair.MyVar = value;
}
}
}
public MyType()
{
MyVar = 0;
}
MyType MyPair { get; set; }
public void SetPair(ref MyType pair)
{
MyPair = pair;
}
public MyType GetPair()
{
return MyPair;
}
}
}
现在,当拖动滑块时,第二个滑块将与第一个滑块一起移动。