0

我是一名 C++ 开发人员,最近转向 C#。我正在开发一个 WPF 应用程序,我需要在其中动态生成按钮、文本框等 Ui 组件。这就是我到目前为止所做的。

XAML 类:

<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">
    <Grid.Resources>
        <convert:BooleanToVisibilityConverter x:Key="booltovisibility"/>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170" />
        <ColumnDefinition />
        <ColumnDefinition Width="130" />
        <ColumnDefinition Width="115" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Content="{Binding ChannelName}" Height="25" Width="120" Name="VoltageLabel" Margin="20,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" />
    <TextBox Grid.Column="1" Text="{Binding VoltageText}" Height="25" Width="65" Name="VoltageBox" Margin="0,0,80,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" Height="25" Width="65" Name="VoltageSetbtn" Margin="80,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <Label Grid.Column="2" Content="{Binding CurrentText}"  Height="25" Width="40" Name="CurrentLabel" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <ToggleButton Grid.Column="3" Content="On"  Height="25" Width="30" Name="VoltageToggleBtn" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>

<Button Content="Bavaria" Name="BavariaBtn" Click="BavariaBtn_Click" />

视图模型类:

public List<VoltageBoardChannel> channelList = null;       

    public List<VoltageBoardChannel> bavaria2Channels = new List<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__IO__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__CODEC__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand},
         new VoltageBoardChannel { ChannelName = "VDD__DAL__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__DPD__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__PLL__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },        
         new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
    };

    private ICommand m_voltageCommand;        

    public List<VoltageBoardChannel> bavaria1Channels = new List<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
         new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }             
    };            

    public VoltageViewModel()
    {
        channelList = new List<VoltageBoardChannel>(0);
        channelList = bavaria1Channels;            
        m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));
    }

    public List<VoltageBoardChannel> VoltageChannelList
    {
        get 
        { 
            return channelList; 
        }

        set
        { 
            channelList = value;
            OnPropertyChanged("ChannelList");
        }
    }        

    public void SetCommandExecute(object voltageText)
    {
        Debug.WriteLine(voltageText);
    }

型号类:

private string mChannelName;
    public string ChannelName
    {
        get; set;
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get; set;
    }

    string voltageText = string.Empty;
    public string VoltageText
    {
        get; set;
    }

    string currentText = "0 V";
    public string CurrentText
    {
        get; set;
    }

    public ICommand VoltageCommand { get; set; }

XAml.cs:

 VoltageViewModel mVoltageViewModel = new VoltageViewModel();

    public VoltageView()
    {
        InitializeComponent();
        this.DataContext = mVoltageViewModel;

        OnChildAdd();
    }
public void OnChildAdd() //Constructor
    {   
        VoltageViewModel mVoltageViewModel = new VoltageViewModel();         
        foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
        {
            VoltageChannelView mVoltageChannelView = new VoltageChannelView();
            mVoltageChannelView.Margin = new Thickness(2);
            mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
            // Some Code
        }
    }

这里它显示了 Bavaria 1 的所有频道,甚至是 one's where available = false。因此,当它为 false 时,它​​会显示文本框、按钮、标签和切换按钮。频道名称是“”。我想实现以下目标:

我这里有 2 个频道,bavaria 1 和 bavaria 2。在启动时 bavaria1 已经显示。在这里,我想检查可用频道并仅将那些添加到我的启动视图中,即available = true应该显示,而当available = false不显示相应元素时。目前甚至available = false显示按钮、文本框、切换按钮,除了标签(频道名称将是“”)。我怎样才能做到这一点?

4

2 回答 2

1

您可以执行类似创建这样的属性的操作:

List<VoltageChannel> AvailableChannels {
  get
  {
     var returned = new List<VoltageChannel>();
     foreach (VoltageChannel vc in VoltageChannelList)
     {
        if (vc.IsAvailable)
          returned.add(vc);
     }
     return vc;
  }
}

然后,将您的视图绑定到此属性,可能使用像这样的 ItemsControl:

<ItemsControl Name="_itemsControl"  ItemsSource="{Binding AvailableChannels}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate><WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      ... (your XAML to show the channels) ...
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

(没有测试你的代码)

于 2012-10-15T12:18:14.780 回答
1

为了使用布尔绑定隐藏 XAML 中的元素,您需要对其进行转换,因为元素的 Visibility 属性不是布尔字段。

<ListBox ItemsSource="{Binding VoltageChannelList}">
  <ListBox.Resources>
    <BooleanToVisibilityConverter x:Key="booltovisibility"/>
  </ListBox.Resources>
  <Grid Visibility="{Binding IsAvailable,
                     Converter={StaticResource booltovisibility}}">
     <!-- controls -->
  </Grid>
</ListBox>

使用默认的BooleanToVisibility转换器。


话虽如此,您可能应该将 VoltageChannelList 更改为ObservableCollection,以便在插入或删除项目时,更改会反映到您的视图中。

另请注意,当您使用自动属性时,您不会创建支持字段

private bool mIsAvailable; // this is not used by the property below
public bool IsAvailable { get; set; }

使用此代码时,mIsAvailable 永远不会从对 IsAvailable 的调用中返回,因为它会创建自己的支持字段。

于 2012-10-15T12:18:48.830 回答