c# - WPF - MVVM - 从 View 模型返回可绑定(bind)控件模板

标签 c# wpf mvvm

我正在尝试在 ListView 中显示可用的 Wifi 网络(使用 ManagedWifi SDK)。

我在我的 View 模型中定义了 WifiNetwork 的 ObservableCollection(包含名称、类型、信号强度(int)等),如下所示

public ObservableCollection<WifiNetwork> AvailableNetworks { get; set; }

我在应用程序的应用程序资源中定义了控制模板(例如 1 个绿色条和 4 个白色条表示信号强度 <= 20%,2 个绿色条和 3 个白色条表示信号强度在 20% 到 40% 之间等)。 xaml如下图

<ControlTemplate x:Key="Signal1">
    <Canvas Width="32" Height="32" Canvas.Left="0" Canvas.Top="0">
        <Rectangle Width="5.4375" Height="11.375" Canvas.Left="0.0937499" Canvas.Top="20.6563" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FF37F307"/>
        <Rectangle Width="6.40625" Height="16" Canvas.Left="5.34375" Canvas.Top="16.0313" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF"/>
        <Path Width="6.88835" Height="21.6562" Canvas.Left="11.75" Canvas.Top="10.375" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF" Data="F1 M 12.2768,10.875L 18.1384,10.875L 18.1115,31.5313L 12.25,31.5313L 12.2768,10.875 Z "/>
        <Rectangle Width="6.78126" Height="26.9687" Canvas.Left="18.5625" Canvas.Top="5.09376" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF"/>
        <Rectangle Width="6.71874" Height="31.8437" Canvas.Left="25.2812" Canvas.Top="0.250002" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF"/>
    </Canvas>
</ControlTemplate>

<ControlTemplate x:Key="Signal2">
    <Canvas Width="32" Height="32" Canvas.Left="0" Canvas.Top="0">
        <Rectangle Width="5.4375" Height="11.375" Canvas.Left="0.0937499" Canvas.Top="20.6563" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FF37F307"/>
        <Rectangle Width="6.40625" Height="16" Canvas.Left="5.34375" Canvas.Top="16.0313" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FF37F307"/>
        <Path Width="6.88835" Height="21.6562" Canvas.Left="11.75" Canvas.Top="10.375" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF" Data="F1 M 12.2768,10.875L 18.1384,10.875L 18.1115,31.5313L 12.25,31.5313L 12.2768,10.875 Z "/>
        <Rectangle Width="6.78126" Height="26.9687" Canvas.Left="18.5625" Canvas.Top="5.09376" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF"/>
        <Rectangle Width="6.71874" Height="31.8437" Canvas.Left="25.2812" Canvas.Top="0.250002" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FFFFFFFF"/>
    </Canvas>
</ControlTemplate>

现在我需要从我的 View 模型返回这个控件模板以将它绑定(bind)到一个按钮以在我的列表框中显示适当的信号强度。

我可能需要进行某种从信号强度 (int) 到控制模板的转换。

有什么建议/代码示例来完成这个任务吗?

最佳答案

这应该引导正确的方向:

    <ItemsControl ItemsSource="{Binding AvailableNetworks }">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Width="20" Height="{Binding SignalStrength,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                     Fill="{Binding Color,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></Rectangle>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

关于c# - WPF - MVVM - 从 View 模型返回可绑定(bind)控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938823/

相关文章:

C# - 邮件确认 Outlook

c# - 为什么我的 ASP.Net MVC 4 WEB API 操作过滤器在运行时被忽略?

c# - 如何通过单击wpf中的按钮来使用TabIndex导航光标?

c# - 在 ViewModel 中更新 View

c# - 如何将列表转换为字符串?

c# - Azure 搜索 SDK 正则表达式?

wpf - MVVM 本地化 - View 中的本地化资源与 ViewModel 中的资源?

wpf - 从弹出的(“child”窗口重新加载父窗口

WPF 窗口透明度(包括非客户区)

wpf - "Invalid Window Handle"退出使用 System.AddIn 的 WPF 应用程序