为了账号安全,请及时绑定邮箱和手机立即绑定

使用 MVVM 自动更新 WPF 中的组合框内容

使用 MVVM 自动更新 WPF 中的组合框内容

C#
慕娘9325324 2021-08-07 16:57:14
我使用的是 Visual Studio 2017 社区版/.NET Framework 4.6.1/WPF。顺便提一句。我使用 MVVM。我的目标是在Combobox 中显示所有可用的串行端口。我已经实现了这一点,但现在我想让组合框在打开下拉菜单或连接新设备时自动更新。因此,我需要一些财产来制作Binding我的ViewModel.在 MSDN 上,我发现 Combobox.DroppedDown-Property 对我来说听起来不错,但我不能使用它,找不到 DroppedDown...(是System.Windows.Forms正确的参考吗?)。你发现我的错误还是你有更好的解决方案?谢谢!<UserControl x:Class="HC_SR04_MPU6050.View.SerialView_MPU_6050"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"          xmlns:local="clr-namespace:HC_SR04_MPU6050.View"         mc:Ignorable="d"><Grid>    <Grid.ColumnDefinitions>        <ColumnDefinition Width="1*"/>        <ColumnDefinition Width="1*"/>        <ColumnDefinition Width="1*"/>        <ColumnDefinition Width="1*"/>        <ColumnDefinition Width="1*"/>    </Grid.ColumnDefinitions>    <Grid.RowDefinitions>        <RowDefinition Height="1*"/>        <RowDefinition Height="1*"/>    </Grid.RowDefinitions>    <Label Grid.Row="0" Content="MPU-6050" HorizontalAlignment="Center"/>    <Label Grid.Row="0" Grid.Column="3" Content="Y-ROT[°]" HorizontalAlignment="Center"/>    <Button Command="{Binding Connect_Clicked}" Grid.Row="1" Grid.Column="0" Content="Connect" Height="20" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6" ToolTip="Opens/Connects the selected COM-PORT"/>    <Button Command="{Binding Measure_Clicked}" Grid.Row="1" Grid.Column="1" Content="MEASURE" Height="20" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6" ToolTip="Starts the Measurement routine"/>    <Button Command="{Binding Stop_Clicked}" Grid.Row="1" Grid.Column="2" Content="STOP" Height="20" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6" ToolTip="Stops the communication/ Closes/Disconnects the selected COM-PORT"/>
查看完整描述

3 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

您还可以查看交互触发器是否可以帮助您。


只需在您的用户控件打开标签中放置类似的内容:


xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

然后在你的组合框标签里面放:


<i.Interaction.Triggers>

  <i:EventTrigger EventName="DropDownOpened">

    <i:InvokeCommandAction Command="{Binding UpdateComnListCommand}"/>

  </i:EventTrigger>

</i:Interaction.Triggers>

这样,每次打开下拉菜单时,您都会收到对 UpdateCommListCommand 的调用。


您可能会遇到一些棘手的刷新问题。我不确定在实际显示打开的组合框之前是否会触发列表更新。


查看完整回答
反对 回复 2021-08-07
  • 3 回答
  • 0 关注
  • 293 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信