所以,我有一个组合框,我想要重复使用它来处理多个数据集,而不是拥有3个单独的组合框。也许这很糟糕,有人可以告诉我。我接受所有的想法和建议。我只是想清理一些代码,认为一个组合框比3个更干净。无论如何,当另一个ComboBox的值更改时,ItemsSource和SelectedItem都应该更改,这会引发未工作的ComboBox的PropertyChanged值。最糟糕的部分是当CurSetpoint.ActLowerModeIsTimerCondition为真时,它总是正确加载SelectedItem,但从那里到CurSetpoint.ActLowerGseMode为True时,组合框没有加载SelectedItem。
这是具有问题的ComboBox的XAML。
这是两个组合框的图片。当模式从Timer更改为Variable时,尽管其绑定不为空且其实例和itemssource实例数据没有更改,但它不会加载任何内容。但是如果我从Variable切换到Timer,则Timer:1会正确显示。 这里是模型代码,用于改变模式下拉框的值。还包括另外两个属性,即所涉及的下拉框的
我确定我可能忽略了一些重要信息,所以如果你们有任何问题或需要详细信息,我会进行更新。
更新:正如悬赏所述。如果我在这里做的不是一个好主意,并且有更好的方法,请有经验的人告诉我为什么以及我应该如何。如果有更好的方法而我正在做的是错误的,我只需要知道。
这是具有问题的ComboBox的XAML。
<ComboBox Grid.Row="1" Grid.Column="1" Margin="5,2" VerticalAlignment="Center" Name="cmbActTimersSetpointsGseVars">
<ComboBox.Style>
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurSetpoint.ActLowerModeIsTimerCondition}" Value="True">
<Setter Property="ItemsSource" Value="{Binding TimerInstances}" />
<Setter Property="SelectedItem" Value="{Binding CurSetpoint.ActLowerTimerInstance, Mode=TwoWay}" />
<Setter Property="DisplayMemberPath" Value="DisplayName"></Setter>
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurSetpoint.ActLowerGseMode}" Value="True">
<Setter Property="ItemsSource" Value="{Binding EnabledGseVars}" />
<Setter Property="SelectedItem" Value="{Binding CurSetpoint.ActLowerGseVar, Mode=TwoWay}" />
<Setter Property="DisplayMemberPath" Value="DisplayName"></Setter>
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurSetpoint.ActModeIsLogicCondition}" Value="True">
<Setter Property="ItemsSource" Value="{Binding SetpointStates}" />
<Setter Property="SelectedItem" Value="{Binding CurSetpoint.ActSetpoint1State, Mode=TwoWay}" />
<Setter Property="DisplayMemberPath" Value="Value"></Setter>
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurSetpoint.ShowActLowerCmbBox}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
这是两个组合框的图片。当模式从Timer更改为Variable时,尽管其绑定不为空且其实例和itemssource实例数据没有更改,但它不会加载任何内容。但是如果我从Variable切换到Timer,则Timer:1会正确显示。 这里是模型代码,用于改变模式下拉框的值。还包括另外两个属性,即所涉及的下拉框的
SelectedItems
。以及ItemsSource
的属性。private VarItem actLowerMode;
public VarItem ActLowerMode
{
get { return this.actLowerMode; }
set
{
if (value != null)
{
var oldValue = this.actLowerMode;
this.actLowerMode = value;
config.ActLowerMode.Value = value.ID;
//if they weren't the same we need to reset the variable name
//Note: 5/21/19 Needs to be this way instead of before because when changing from Timer->GseVariable it wouldn't change the config value because it
//thought it was still a timer condition because the value hadn't been changed yet.
if (oldValue != null && (oldValue.CheckAttribute("timer") != value.CheckAttribute("timer")))
{
if (value.CheckAttribute("timer"))
{
ActLowerTimerInstance = model.TimerInstances.First();
}
else
{
ActLowerVarName = "";
if (GseMode)
{
ActLowerGseVar = model.EnabledGseVars.FirstOrDefault();
}
}
}
RaisePropertyChanged("ActLowerMode");
RaisePropertyChanged("HasActLowerScale");
RaisePropertyChanged("ActLowerGseMode");
RaisePropertyChanged("HasActLowerVarName");
RaisePropertyChanged("ActLowerModeIsConstant");
RaisePropertyChanged("ActLowerRow1Label");
RaisePropertyChanged("ActLowerModeIsTimerCondition");
RaisePropertyChanged("ShowActLowerConstTextBox");
RaisePropertyChanged("ShowActLowerCmbBox");
RaisePropertyChanged("ShowActLowerRow1Label");
if (GseMode)
{
RaisePropertyChanged("ActLowerGseMode");
}
}
}
}
private GseVariableModel actLowerGseVar;
public GseVariableModel ActLowerGseVar
{
get { return this.actLowerGseVar; }
set
{
if (value != null)
{
this.actLowerGseVar = value;
if (!ActLowerModeIsTimerCondition)//only changing the config value if its not set to timer
{
config.ActLowerVarName.Value = value.Number.ToString();
}
RaisePropertyChanged("ActLowerGseVar");
}
}
}
private INumberedSelection actLowerTimerInstance;
public INumberedSelection ActLowerTimerInstance
{
get { return this.actLowerTimerInstance; }
set
{
if (value != null)
{
this.actLowerTimerInstance = value;
config.ActLowerVarName.Value = value.Number.ToString();
RaisePropertyChanged("ActLowerTimerInstance");
}
}
}
public ObservableCollection<INumberedSelection> TimerInstances { get { return this.timerInstances; } }
public ObservableCollection<GseVariableModel> EnabledGseVars
{
get
{
return enabledGseVariables;
}
}
我确定我可能忽略了一些重要信息,所以如果你们有任何问题或需要详细信息,我会进行更新。
更新:正如悬赏所述。如果我在这里做的不是一个好主意,并且有更好的方法,请有经验的人告诉我为什么以及我应该如何。如果有更好的方法而我正在做的是错误的,我只需要知道。
true
的3个DataTriggers是否会有问题。例如,当“ActModeIsLogicCondition”和“ActLowerModeIsTimerCondition”都为真时会发生什么?或者这不可能发生吗? - Ginger Ninja