DataGrid:如何在不操作GUI的情况下以编程方式选择DataGrid ComboBox项?

时间:2013-12-13 02:04:43

标签: wpf xaml datagrid combobox

之前我问过这个问题并且有一个有效的解决方案:

See Answer by srithar here

成为XAML / WPF的新手,那是我本能首选的解决方案。然而,专家们继续告诉我们:“不要操纵GUI!处理模型。”

这是相关的XAML代码:

<DataGridTemplateColumn Header=" Top Plate Thickness ">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox
                    ItemsSource="{Binding Gauge, Mode=TwoWay}"
                    DisplayMemberPath="Thickness"
                    SelectedItem="{Binding TopPlateThickness, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ComboBoxLeft_SelectionChanged">
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

它目前支持禁用菜单项的功能。这是守则背后:

static List<string> caliber = new List<string> { "0.3750", "0.5000", "0.6250", "0.7500", "1.0000" };


string left  = "";
string right = "";
PlateThicknesses(ref left, ref right, rnd);
Selectable top = new Selectable(left);
Selectable bot = new Selectable(right);


GridsModel gm = new GridsModel()
{
    OuterDiameter        = outerDiameter,
    InnerDiameter        = innerDiameter,
    TopPlateThickness    = top,
    BottomPlateThickness = bot
};


static void PlateThicknesses(ref string leftComboBoxIndex, ref string rightComboBoxIndex)
{
    int a = rnd.Next(7);
    int b = rnd.Next(7);

    if (a > b)
    {
        leftComboBoxIndex  = caliber[a];
        rightComboBoxIndex = caliber[b];
    }
    else
    {
        leftComboBoxIndex  = caliber[b];
        rightComboBoxIndex = caliber[b];
    }
}

型号:

public class GridsModel : PropertyChangedBase
{
    public string Item                    { get; set; }
    public string Description             { get; set; }

    private int _Quantity;
    public  int  Quantity
    {
        get
        {
            return _Quantity;
        }
        set
        {
            if (_Quantity != value)
            {
                _Quantity = value;
                RaisePropertyChanged("Quantity");
            }
        }
    }

    private int _QtyOfFound;
    public  int  QtyOfFound
    {
        get
        {
            return _QtyOfFound;
        }
        set
        {
            if (_QtyOfFound != value)
            {
                _QtyOfFound = value;
                RaisePropertyChanged("QtyOfFound");
            }
        }
    }

    public double MaxBoltLoad             { get; set; }
    public int    NumberOfBolts           { get; set; }
    public double Separation              { get; set; }

    private double _Length;
    public  double  Length
    {
        get
        {
            return _Length;
        }
        set
        {
            if (_Length != value)
            {
                _Length = value;
                RaisePropertyChanged("Length");
            }
        }
    }

    public double Gamma                   { get; set; }
    public double Beta                    { get; set; }
    public double OuterDiameter           { get; set; }
    public double InnerDiameter           { get; set; }

    public List<Selectable> Gauge         { get; set; }           // Gauche is "Left" in French

    private Selectable _TopPlateThickness;
    public  Selectable  TopPlateThickness
    {
        get
        {
            return _TopPlateThickness;
        }
        set
        {
            if (_TopPlateThickness != value)
            {
                _TopPlateThickness = value;
                RaisePropertyChanged("TopPlateThickness");
            }
        }
    }

    public List<Selectable> Caliber       { get; set; }           // The Right Stuff

    private Selectable _BottomPlateThickness;
    public  Selectable  BottomPlateThickness
    {
        get
        {
            return _BottomPlateThickness;
        }
        set
        {
            if (_BottomPlateThickness != value)
            {
                _BottomPlateThickness = value;
                RaisePropertyChanged("BottomPlateThickness");
            }
        }
    }

    public double BoltCircleDiameter      { get; set; }

    private double _TemplateSetWeight;
    public  double  TemplateSetWeight
    {
        get
        {
            return _TemplateSetWeight;
        }
        set
        {
            if (_TemplateSetWeight != value)
            {
                _TemplateSetWeight = value;
                RaisePropertyChanged("TemplateSetWeight");
            }
        }
    }

    public double AnchorBoltSetWeight     { get; set; }

    private double _WholeAnchorCageWeight;
    public  double  WholeAnchorCageWeight
    {
        get
        {
            return _WholeAnchorCageWeight;
        }
        set
        {
            if (_WholeAnchorCageWeight != value)
            {
                _WholeAnchorCageWeight = value;
                RaisePropertyChanged("WholeAnchorCageWeight");
            }
        }
    }

    public GridsModel()
    {
        Gauge = new List<Selectable>  {  new Selectable("0.3750"),
                                         new Selectable("0.4375"),
                                         new Selectable("0.5000"),
                                         new Selectable("0.6250"),
                                         new Selectable("0.7500"),
                                         new Selectable("0.8650"),
                                         new Selectable("1.0000") };

        Caliber = new List<Selectable> { new Selectable("0.3750"),
                                         new Selectable("0.4370"),
                                         new Selectable("0.5000"),
                                         new Selectable("0.6250"),
                                         new Selectable("0.7500"),
                                         new Selectable("0.8650"),
                                         new Selectable("1.0000") };
    }
}

可选择的课程:

public class Selectable : PropertyChangedBase
{
    private string _Thickness;
    public  string  Thickness
    {
        get
        {
            return _Thickness;
        }
        set
        {
            if (_Thickness != value)
            {
                _Thickness = value;
                RaisePropertyChanged("Thickness");
            }
        }
    }

    private bool _Enabled;
    public  bool  Enabled
    {
        get
        {
            return _Enabled;
        }
        set
        {
            if (_Enabled != value)
            {
                _Enabled = value;
                RaisePropertyChanged("Enabled");
            }
        }
    }

    public Selectable(string thickness)
    {
        Thickness = thickness;
        Enabled = true;
    }
}

使用调试代码我可以看到所需的属性实际上被分配了所需的值,但是可视UI无法显示它们。

TIA

0 个答案:

没有答案