首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能在WPF中动态生成的UI组件中设置不同的值

问未能在WPF中动态生成的UI组件中设置不同的值
EN

Stack Overflow用户
提问于 2012-10-23 11:05:54
回答 1查看 244关注 0票数 0

我正在处理一个WPF应用程序,在其中我需要动态生成两个组框,其中有一个单选按钮、文本框和一个组合框。嗯,我遇到了一个棘手的情况,在两个组框中,价值观需要不同。

我有两个xaml文件PCM2PDMView.xaml和PCM2PDMWidgetView.xaml & viewmodel类。

PCM2PDMView.xaml:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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 });            
    }

这将生成两个组框,并在其中包含关联的UI控件。

PCM2PDMWidgetViewModel.cs:

代码语言:javascript
复制
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;}

Requirement:

  1. 现在,如果您注意到Grid.Row=1,您会发现一个名为Port B的无线电按钮,我的要求是在我的第一个组盒中有Port B,在第二个组框中有Port C。这两种功能(按钮单击方法)都是通用的,但名称必须不同。我们如何才能做到这一点?
  2. 在我的组合框中,我添加了3个项目。在这里,我想在第一组框中的组合框中添加Normal和Mode 1 - PCM + PDM,在第二组盒中添加Normal、Mode 1 - PCM + PDM和Mode 2 - PDM Only。如何做到这一点?)
  3. 如果您注意到Grid.Row=3,您将发现PCM2PDMEnable切换按钮。我希望这个切换按钮在我的第二个组框中可见,而不是在我的第一个组框中。我怎么能这么做?
  4. Grid.Row=4中的Textbox有一个textbox,其值为(例如0x5)。我需要将这个文本框的文本设置为Groupbox2的Groupbox 1中的0x5和textbox中的0x7。怎么做?

(请帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-23 11:15:23

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

代码语言:javascript
复制
PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 0", ID = 0, RadioButtonName = "Port B" });
        PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 1", ID = 1, RadioButtonName = "Port C" });            

In PCM2PDMWidgetView XAML:

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

代码语言:javascript
复制
<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(项目);}

在创建"PCM2PDMWidgetViewModel“实例时,将列表作为构造函数参数发送(请参阅以下代码)

代码语言:javascript
复制
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

您可以用与radion按钮名相同的方式来修复这个问题。

为"MemAddPoint“属性设置值的方式与对描述和ID的设置相同。

For 3:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13029083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档