【问题标题】:Failing To set different values in Dynamically Generated UI Components in WPF无法在 WPF 中动态生成的 UI 组件中设置不同的值
【发布时间】:2012-10-13 07:18:51
【问题描述】:

我正在开发一个 WPF 应用程序,我需要在其中动态生成 2 个组框,其中包含一个单选按钮、文本框和一个组合框。好吧,我遇到了一个棘手的情况,即两个组框中的值需要不同。

我有两个 xaml 文件 PCM2PDMView.xamlPCM2PDMWidgetView.xaml 以及 viewmodel 类。

PCM2PDMView.xaml:

<UserControl.Resources>
    <DataTemplate x:Key="PCM2PDMDataTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Description}" Margin="5,5,0,0"/>
            <local:PCM2PDMWidgetView Margin="5,10,5,5"/>
        </WrapPanel>
    </DataTemplate>
</UserControl.Resources>

<Grid Style="{DynamicResource styleBackground}" >
    <ItemsControl ItemTemplate="{StaticResource PCM2PDMDataTemplate}" ItemsSource="{Binding PCM2PDMWidgets}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

PCM2PDMWidgetView.xaml:

<Grid>
    <GroupBox Height="Auto" HorizontalAlignment="Stretch" Margin="5" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
        <Grid>             
            <Grid Grid.Row="1">                   
                <RadioButton Grid.Column="1" Content="Port B" Command="{Binding PortCommand}" IsChecked="{Binding PortCheck}" Height="20" Width="60" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCM2PDMEnableRadioBtn" VerticalAlignment="Center" />                    
            </Grid>

            <Grid Grid.Row="2">                  

                <ComboBox Grid.Column="1" Height="20" ItemsSource="{Binding PortModeList}" SelectedItem="{Binding SelectedPortModeList, Mode=OneWayToSource}" SelectedIndex="0" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PortModeCombo" VerticalAlignment="Center" Width="110" />
            </Grid>

            <Grid Grid.Row="3">                    
                <ToggleButton Grid.Column="0" Content="Soft Reset" Height="25" Width="105" HorizontalAlignment="Center" Margin="0,0,0,0" Name="SoftResetRadioBtn" VerticalAlignment="Center" />
                <ToggleButton Grid.Column="1" Command="{Binding PCM2PDMEnableCommand}" IsChecked="{Binding PCM2PDMCheck}" Content="PCM2PDM Enable" Height="25" Width="110" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCM2PDMRadioBtn" VerticalAlignment="Center" />
            </Grid> 

            <Grid Grid.Row="4">                 
                <TextBox Height="25" IsReadOnly="True" Text="{Binding MemAddPoint}" Margin="0" Name="SetRead" />               
            </Grid>
       </Grid>
   </GroupBox>
</Grid>

PCM2PDmViewModel.cs:

public ObservableCollection<PCM2PDMWidgetViewModel> PCM2PDMWidgets { get; set; }

    public PCM2PDMViewModel()
    {
        PCM2PDMWidgets = new ObservableCollection<PCM2PDMWidgetViewModel>();
        PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 0", ID = 0 });
        PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 1", ID = 1 });            
    }

这会生成 2 个组框,其中包含相关的 UI 控件。

PCM2PDMWidgetViewModel.cs:

public ObservableCollection<string> _PortModeList = _PortModeList = new ObservableCollection<string>();
_PortModeList.Add("Normal"); //Adding in Constructor
_PortModeList.Add("Mode 1 - PCM + PDM");
_PortModeList.Add("Mode 2 - PDM only");

public ObservableCollection<string> PortModeList
    {
        get { return _PortModeList; }
        set
        {
            _PortModeList = value;
            OnPropertyChanged("PortModeList");
        }
    }

    private string _selectedPortModeList;
    public string SelectedPortModeList
    {
        get { return _selectedPortModeList; }
        set
        {
            _selectedPortModeList = value;                
            OnPropertyChanged("SelectedPortModeList");
        }
    }

    public string Description {get; set;}      

    public int ID {get; set;}

    public string MemAddPoint {get; set;}

要求:

    1234563功能(按钮单击方法)对两者都是通用的,但名称必须不同。我们怎样才能做到这一点?
  1. 在我的组合框中,我添加了 3 个项目。在这里,我想在第一个组合框中的组合框中添加NormalMode 1 - PCM + PDM,并在第二个组合框中的组合框中添加NormalMode 1 - PCM + PDMMode 2 - PDM Only。怎样才能做到这一点? :)

  2. 如果您注意到Grid.Row=3,您会发现PCM2PDMEnable 切换按钮。我想让这个切换按钮在我的第二个 groupbox 中而不是在我的第一个 groupbox 中可见。我该怎么做?

  3. Grid.Row=4 中的文本框有一个值为(例如 0x5)的文本框。我需要将此文本框的文本设置为 Groupbox 1 中的 0x5 和 Groupbox2 的 Textbox 中的 0x7。该怎么做?

请帮忙:)

【问题讨论】:

    标签: c# .net wpf dynamic mvvm


    【解决方案1】:

    在您的“PCM2PDMWidgetViewModel”中创建一个属性,例如“RadioButtonName”,并在创建“PCM2PDMWidgetViewModel”实例时将该属性设置为您想要的名称......类似这样的东西

    PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 0", ID = 0, RadioButtonName = "Port B" });
            PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 1", ID = 1, RadioButtonName = "Port C" });            
    

    在 PCM2PDMWidgetView XAML 中:

    将单选按钮内容更改为绑定“Content="{Binding RadioButtonName}"”

    <Grid Grid.Row="1">                   
                    <RadioButton Grid.Column="1" Content="{Binding RadioButtonName}" Command="{Binding PortCommand}" IsChecked="{Binding PortCheck}" Height="20" Width="60" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCM2PDMEnableRadioBtn" VerticalAlignment="Center" />   
    
    
            </Grid>
    

    而不是在构造函数中添加以下元素..将它们作为列表作为参数发送给构造函数..

    您的代码

    _PortModeList.Add("正常"); //在构造函数中添加 _PortModeList.Add("模式 1 - PCM + PDM"); _PortModeList.Add("模式 2 - 仅 PDM");

    解决方案 2

    公共 PCM2PDMWidgetViewModel(列出 comboBoxItems) { foreach(comboBoxItems 中的项目) { _PortModeList.Add(item); } }

    在创建“PCM2PDMWidgetViewModel”实例时发送列表作为构造函数参数(看下面的代码)

    PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel (new List<string>{"Normal","Mode 1 - PCM + PDM"}) { Description = "PCM2PDM 0", ID = 0, RadioButtonName = "Port B" });
            PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel(new List<string>{"Normal","Mode 1 - PCM + PDM","Mode 2"}) { Description = "PCM2PDM 1", ID = 1, RadioButtonName = "Port C" });  
    

    4人用

    您可以使用与单选按钮名称相同的方式来解决此问题..

    以与描述和 ID 相同的方式设置“MemAddPoint”属性的值。由于此属性已绑定到文本框,因此您无需更改 xaml 绑定

    对于 3:

    您必须在“可见性”类型的“PCM2PDMWidgetViewModel”中创建一个新属性,并将其绑定到切换按钮可见性属性。创建实例时设置属性值

    【讨论】:

    • 查询 2 的解决方案,但抛出错误。我做了以下事情:public PCM2PDMWidgetViewModel(List comboBoxItems) { foreach(string item in comboBoxItems) { _PortModeList.Add(item); } } 在构造函数中,在 PCM2PDMViewModel 类中进行以下操作PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel (new List&lt;string&gt; { "Normal", "Mode 1 - PCM + PDM" }) { Description = "PCM2PDM 0", ID = 0, PortName = "Port B" }); PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel (new List&lt;string&gt; { "Normal", "Mode 1 - PCM + PDM", "Mode 2" }) { Description = "PCM2PDM 1", ID = 1, PortName = "Port C" });
    • PCM2PDMWidgetViewModel(List comboBoxItems).. 应该是 PCM2PDMWidgetViewModel(List comboBoxItems).. 请发布您遇到的错误
    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多