我有一个列表框,其中包含一个字符串列表,格式为"02x06"..总是两位数,小写x,两位数,不带空格。它被绑定到两个文本框,一个保存行(在本例中为02),另一个保存列(06)。
这个工作很好。
现在我需要将row文本框绑定到代码中的row属性,并将col文本框绑定到代码中的col属性。我试图通过MultiBinding:
来做到这一点XAML:<Windows.Resources>
<local:DiePrintConverter x:Key="DiePrintConverter"/>
</Window.Resources>
<ListBox
Name="lbxUninspectedPrints"
ItemsSource="{Binding UninspectedPrintList}"
Margin="5"
Background="MidnightBlue"/>
<TextBox
Name="txtCol"
Style="{StaticResource txtStyle}">
<TextBox.Text>
<MultiBinding Converter="{StaticResource DiePrintConverter}">
<Binding
Path="SelectedValue"
ConverterParameter="1"
ElementName="lbxUninspectedPrints"
Mode="OneWay"/>
<Binding
Path="Col"
Mode="OneWayToSource"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
<TextBox
Name="txtRow"
Style="{StaticResource txtStyle}">
<TextBox.Text>
<MultiBinding Converter="{StaticResource DiePrintConverter}">
<Binding
Path="SelectedValue"
ConverterParameter="0"
ElementName="lbxUninspectedPrints"
Mode="OneWay"/>
<Binding
Path="Row"
Mode="OneWayToSource"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
背后的代码:
lbxUninspectedPrints.DataContext = wtw;
txtRow.DataContext = wtw.DiePrintNav;
txtCol.DataContext = wtw.DiePrintNav;
DiePrintConverter类:
public class DiePrintConverter : IMultiValueConverter
{
bool row = true;
public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
{
if (value[0] == null)
{
return "00";
}
var values = value[0].ToString().Split(new string[] { "x" }, StringSplitOptions.None);
int x = 0;
for (int i = 0; i < values.Count(); i++)
{
if (values[i].ToString().Count() == 1)
{
values[i] = "0" + values[i];
}
}
if (row)
{
row = false;
return values[0].ToString();
}
else
{
row = true;
return values[1].ToString();
}
return "00";
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
DiePrintNavigation类:
public class DiePrintNavigation
{
#region Fields
private string row;
private string col;
private DiePrint diePrint;
private DelegateCommand moveUpCommand;
private DelegateCommand moveDownCommand;
private DelegateCommand moveLeftCommand;
private DelegateCommand moveRightCommand;
private DelegateCommand loadMapCommand;
private WaferTrackerWindowViewModel wtw;
#endregion
#region Constructor
public DiePrintNavigation() { }
public DiePrintNavigation(WaferTrackerWindowViewModel wtw)
{
this.wtw = wtw;
}
#endregion
#region Properties
public string Row
{
set
{
row = value;
}
}
public string Col
{
set
{
col = value;
}
}
public DiePrint DiePrint
{
get
{
return diePrint;
}
set
{
diePrint = value;
}
}
public ICommand MoveUpCommand
{
get
{
if (moveUpCommand == null)
{
moveUpCommand = new DelegateCommand(MoveUp);
}
return moveUpCommand;
}
}
public ICommand MoveDownCommand
{
get
{
if (moveDownCommand == null)
{
moveDownCommand = new DelegateCommand(MoveDown);
}
return moveDownCommand;
}
}
public ICommand MoveLeftCommand
{
get
{
if (moveLeftCommand == null)
{
moveLeftCommand = new DelegateCommand(MoveLeft);
}
return moveLeftCommand;
}
}
public ICommand MoveRightCommand
{
get
{
if (moveRightCommand == null)
{
moveRightCommand = new DelegateCommand(MoveRight);
}
return moveRightCommand;
}
}
public ICommand LoadMapCommand
{
get
{
if (loadMapCommand == null)
{
loadMapCommand = new DelegateCommand(LoadMap);
}
return loadMapCommand;
}
}
#endregion
#region Methods
private void Move()
{
DiePrintQueries diePrintQueries = new DiePrintQueries(
DataLibrary.SingulationOne,
DataLibrary.MasksInfo);
diePrint.Name = diePrint.Row.ToString() + "x" + diePrint.Col.ToString();
try
{
diePrint.Mask = diePrintQueries.GetMask(diePrint.Name);
diePrint.LasersPerDie = diePrintQueries.GetLasersPerDie(diePrint.Mask);
diePrint.DieTable = diePrintQueries.GetListOfDie(diePrint.Name);
wtw.WaferMap.DiePrint = diePrint;
wtw.WaferMap.DrawMap();
}
catch (InvalidOperationException)
{
MessageBox.Show(
"Die Print " + diePrint.Name + " does not exist on this wafer.n" +
"Please move to a die print that exists.",
"Non-Existent Die Print", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void MoveUp()
{
// TODO: Catch all these Move Errors!!!
try
{
diePrint.GetRowAndColumn(diePrint.Name);
diePrint.Row = diePrint.Row - 1;
Move();
}
catch (NullReferenceException)
{
}
}
public void MoveDown()
{
diePrint.GetRowAndColumn(diePrint.Name);
diePrint.Row = diePrint.Row + 1;
Move();
}
public void MoveRight()
{
diePrint.GetRowAndColumn(diePrint.Name);
diePrint.Col = diePrint.Col + 1;
Move();
}
public void MoveLeft()
{
diePrint.GetRowAndColumn(diePrint.Name);
diePrint.Col = diePrint.Col - 1;
Move();
}
public void LoadMap()
{
if (diePrint == null)
{
DiePrintQueries diePrintQueries = new DiePrintQueries(
DataLibrary.SingulationOne,
DataLibrary.MasksInfo);
if (this.row.StartsWith("0"))
{
row = row.Remove(0, 1);
}
if (this.col.StartsWith("0"))
{
col = col.Remove(0, 1);
}
diePrint = new DiePrint(this.row + "x" + this.col);
diePrint.Mask = diePrintQueries.GetMask(diePrint.Name);
diePrint.DieTable = diePrintQueries.GetListOfDie(diePrint.Name);
// TODO: dupCode(1)
// Then map the die print
wtw.WaferMap = new DiePrintMapViewModel(diePrint);
wtw.WaferMap.NotifyCanvas += wtw.Update;
wtw.WaferMap.NotifyBluetape += wtw.Update;
wtw.WaferMap.NotifyDieprint += wtw.Update;
wtw.WaferMap.DrawMap();
wtw.FailureCodeManager.DiePrintMap = (DiePrintMapViewModel) wtw.WaferMap;
}
diePrint.Row = Convert.ToInt32(this.row);
diePrint.Col = Convert.ToInt32(this.col);
Move();
}
#endregion
}
wtw是包含所有这些组件的窗口的视图模型。DiePrintNav是一个包含Row和Col只写属性的类,当txtRow和txtCol值发生变化时,这些属性需要更新。wtw视图模型对象包含DiePrintNav属性,是文本框的数据上下文。UninspectedPrintList列表框的数据上下文也是wtw对象,并且绑定到wtw的UninspectedPrintList属性。
列表框填充正确。然后我可以单击列表框中的任何内容,它被镜像到txtRow和txtCol框中也很好,因此multivalueconverter类也可以正常工作。
问题是,每当txtRow和txtCol框中的任何更改时,它们各自的属性都不会更新。
发生什么事了??
任何帮助将非常感激!
谢谢,
凯尔
也许你的类DiePrintNav
必须实现INotifyPropertyChanged
接口。不是吗?这可能是原因。请核对一下。
编辑
我现在明白了,你想从视图更新你的模型属性。问题似乎是,你需要定义你的MultiValueConverter的ConvertBack方法,以这种方式,视图将能够更新所有的属性时,它的变化。这里的另一件事是绑定应该是Mode=TwoWay
,因为,即使你不想从模型更新视图,第一次加载模型时,你可能希望视图显示旧的模型值,在这种情况下,需要双向绑定。希望这个更新对你有帮助。
编辑
请查看MSDN multivalueconverter接口文章,这里解释了IMultivalueConverter
接口的工作方式,希望有所帮助:)