我正在开发MVVM应用程序,对于将项目添加到放置在DataGridTemplateColumn中的组合框中,我有一些逻辑。每个生成的组合框都绑定到一个Observablecollection,当Datagridrow更改时,该Observablecollection会刷新。组合框DropDownList中只有两项。由于集合是每一行所需的类的属性,因此ComboBox绑定和DropDownList项将按预期显示。索引为[0]的是默认值,可编辑。在索引 [1] 中,我具有提到的类的某些属性的SumProduct-可以工作并显示在特定ComboBox的DropDownList中。
我想达到的目的是当调用一种计算SumProducts的方法时,在组合框上显示此 [1] 项。
XAML:
<DataGridTemplateColumn Header="PRĄD POJEMNOŚCIOWY [A]" HeaderStyle="{StaticResource PRAD_POJEMNOSCIOWY}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<ComboBox Name="PradPojemnosciowyComboBox"
SelectedValue="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=LiniaWyComboBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEditable="True"
IsReadOnly="False"
Text="{Binding Prad_pojemnosciowy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsTextSearchEnabled="False"
IsSynchronizedWithCurrentItem="False"
PreviewKeyDown="PradPojemnosciowyComboBox_OnPreviewKeyDown">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="SelectedValue" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
....
///触发MainWindow.xaml.cs中的方法的属性之一
<DataGridTemplateColumn Header="DŁUGOŚĆ [km]" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBox Name="dlugoscTextBox"
Background="{StaticResource navyColor}" Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
TextChanged="TextBox_OnTextChanged">
<TextBox.Text>
<Binding Path="długość"
Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validator:DoubleValidator Min="0" Max="1000"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
PradPojemnosciowyComboBox_OnPreviewKeyDown:
private void PradPojemnosciowyComboBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
ComboBox tb = (ComboBox)sender;
if (tb.Text != null)
{
var value = 0.0;
bool isTry = Double.TryParse(tb.Text, out value);
if (isTry)
{
SelectedItem = value;
var lineSelected = (modelGPZ.GetLineWyList().FirstOrDefault(x => x.isSelected == true));
if (lineSelected != null)
{
var u = e.OriginalSource as UIElement;
if (e.Key == Key.Enter && u != null)
{
lineSelected.Prad_pojemnosciowy = value;
e.Handled = true;
u.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
}
}
refreshLineWy();
if (tb.Text != lineSelected.LiniaWyComboBox[0].ToString())
{
tb.Text = lineSelected.LiniaWyComboBox[0].ToString();
OnPropertyChanged("SelectedItem");
OnPropertyChanged("LiniaWyComboBox");
}
}
}
}
被调用的方法对所有必要值进行计数,并在特定的ComboBox-DropDownList中的项[1]中显示它们。再一次-我需要的是我想在更新后或根据我的想法显示ComboBox中的 [1] 项-调用更新值的方法。