【发布时间】:2019-05-22 20:08:29
【问题描述】:
所以我有一个组合框,我想为多组数据重复使用,而不是拥有 3 个单独的组合框。也许这很糟糕,有人可以告诉我。我对所有想法和建议持开放态度。我只是想清理一些代码,并认为一个组合框而不是 3 更干净。无论如何,ItemsSource 和 SelectedItem 都应该在另一个 ComboBox'svalue 发生变化时发生变化,这会提高不起作用的 ComboBox 的 Property Changed 值。最糟糕的部分是当CurSetpoint.ActLowerModeIsTimerCondition为真时,它总是正确加载SelectedItem,但是当从那个到CurSetpoint.ActLowerGseMode为真时,组合框没有加载SelectedItem。
这是有问题的 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 时,它不会加载任何内容,尽管它的绑定不是 null 并且它的实例和 itemssource 实例数据没有改变。但是如果我从变量转到计时器,计时器:1 会正确显示。
这是模式组合框值被更改后的模型代码。连同其他两个属性,即相关 ComboBox 的 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;
}
}
我确定我可能忽略了一些重要信息,因此我会根据你们的任何问题或需要的详细信息进行更新。
更新:只是想按照赏金中的说明添加。如果我在这里做的不是一个好主意,并且有更好的方法,请有经验的人告诉我为什么以及如何做。如果有更好的方法,而我做的不好,我只需要知道。
【问题讨论】:
-
欢迎加入WPF Chat Room讨论...
-
是否可以将
CurSetpoint切换为使用枚举而不是 3 个布尔值?我很好奇只有true的条件检查的 3 个 DataTriggers 是否可能是一个问题。 IE。当ActModeIsLogicCondition和ActLowerModeIsTimerCondition都为真时会发生什么?还是那不可能? -
@GingerNinja 应该不可能,但会仔细检查逻辑并回复您。通过将其切换为枚举,您的说法就像拥有 CurSetpoint.ActLowerEnumValue 之类的东西并让它寻找说 1、2、3 或 4 类型的场景?
-
@Birdbuster 是的。因此,您的 DataTrigger 将像 switch 语句一样工作,而不是连续的
if语句。通常,如果我在一组触发器中有两个以上的选项,我会尝试使用枚举。
标签: c# wpf selecteditem datatrigger itemssource