亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 323 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號