MultiBinding TextBox不更新源属性

时间:2013-03-13 19:25:10

标签: c# .net wpf binding multibinding

我有一个列表框,其中包含“02x06”形式的字符串列表。总是两位数字,小写字母x,两位数字,没有空格。这绑定到两个文本框,一个保存行(在本例中为02),另一个保存列(06)。

这完全没问题。

现在我需要将行文本框绑定到代码中的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 Class:

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属性,并且是文本框的数据上下文。未经检查的打印列表框的数据上下文也是wtw对象,并绑定到wtw的UninspectedPrintList属性。

列表框已正确填写。然后我可以单击列表框中的任何内容,它也会被镜像到txtRow和txtCol框中,所以IMultiValueConverter类也正常工作。

问题是,只要txtRow和txtCol框中发生任何变化,它们各自的属性就不会更新。

发生了什么事?

任何帮助将不胜感激!

谢谢,

凯尔

1 个答案:

答案 0 :(得分:0)

也许您的班级DiePrintNav必须实施INotifyPropertyChanged界面。可以?这可能是原因。请检查一下。

修改


我现在明白了,您想要从视图更新模型属性。问题似乎是您需要定义MultiValueConverter的ConvertBack方法,这样视图将能够在更改时更新所有属性。另一件事是绑定应该是Mode=TwoWay,因为,即使您不想从模型更新视图,第一次加载模型时,您可能希望视图显示旧的模型值,在这种情况下需要双向绑定。希望此更新有助于......

修改


请查看MSDN IMultivalueConverter Interface文章,此处解释IMultivalueConverter界面的工作方式,希望有所帮助:)