我使用的是 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 的調用。
您可能會遇到一些棘手的刷新問題。我不確定在實際顯示打開的組合框之前是否會觸發列表更新。
- 3 回答
- 0 關注
- 323 瀏覽
添加回答
舉報
0/150
提交
取消