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

我使用的是 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"/>


慕娘9325324
浏览 294回答 3
3回答

冉冉说

您还可以查看交互触发器是否可以帮助您。只需在您的用户控件打开标签中放置类似的内容:xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"然后在你的组合框标签里面放:<i.Interaction.Triggers>&nbsp; <i:EventTrigger EventName="DropDownOpened">&nbsp; &nbsp; <i:InvokeCommandAction Command="{Binding UpdateComnListCommand}"/>&nbsp; </i:EventTrigger></i:Interaction.Triggers>这样,每次打开下拉菜单时,您都会收到对 UpdateCommListCommand 的调用。您可能会遇到一些棘手的刷新问题。我不确定在实际显示打开的组合框之前是否会触发列表更新。
打开App,查看更多内容
随时随地看视频慕课网APP