Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法仅显示视图中的可用项目 我是C++开发人员,最近转移到C语言。我正在开发一个WPF应用程序,我需要在其中动态生成按钮、文本框等Ui组件。到目前为止,我就是这样做的_C#_Wpf_Button_Mvvm_Viewmodel - Fatal编程技术网

C# 无法仅显示视图中的可用项目 我是C++开发人员,最近转移到C语言。我正在开发一个WPF应用程序,我需要在其中动态生成按钮、文本框等Ui组件。到目前为止,我就是这样做的

C# 无法仅显示视图中的可用项目 我是C++开发人员,最近转移到C语言。我正在开发一个WPF应用程序,我需要在其中动态生成按钮、文本框等Ui组件。到目前为止,我就是这样做的,c#,wpf,button,mvvm,viewmodel,C#,Wpf,Button,Mvvm,Viewmodel,XAML类: <Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}"> <Grid.Resources> <convert:BooleanToVisibilityConverter x:Key="booltovisibility"/> </Grid.Resources> <Grid.Col

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
        }
    }
在这里,它显示了巴伐利亚1号的所有通道,甚至是其中一个通道的where
available=false
。因此,当其为false时,将显示文本框、按钮、标签和切换按钮。频道名称为“”。我想实现以下目标:


我这里有两个频道,巴伐利亚1和巴伐利亚2。启动时巴伐利亚1已经显示。在这里,我想检查可用通道,并仅在启动时将其添加到我的视图中,即应显示
available=true
,当
available=false
时,不应显示相应的元素。当前偶数
available=false
显示为按钮、文本框、切换按钮,标签除外(Channelname将为“”)。我怎样才能做到这一点

您可以像这样创建一个属性:

List<VoltageChannel> AvailableChannels {
  get
  {
     var returned = new List<VoltageChannel>();
     foreach (VoltageChannel vc in VoltageChannelList)
     {
        if (vc.IsAvailable)
          returned.add(vc);
     }
     return vc;
  }
}
<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>
列出可用通道{
得到
{
返回的变量=新列表();
foreach(电压通道列表中的电压通道vc)
{
如果(vc.I可用)
返回。添加(vc);
}
返回vc;
}
}
然后,将视图绑定到此属性,可以使用如下ItemsControl:

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

... (显示频道的XAML)。。。

(未测试代码)

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

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

使用此代码时,调用IsAvailable不会返回Misavable,因为它会创建自己的支持字段。

能否显示完整的视图XAML?上面的XAML似乎只是单个元素的代码,而不是周围视图的代码,问题似乎是这样的;频道列表=巴伐利亚州1频道;这是一些奇怪的代码。你不需要“新建”一个列表,你也会分配一些完全不同的东西。这是“草率”的内存管理。@Akku:更新了xaml。检查我不知道BooltVisibilityConverter的网格资源是否与静态资源冲突。我将只使用静态的,并在App.xaml文件中定义它。如果这不能解决问题,我想知道网格如何知道它的模型对象是什么。您不应该将网格的DataContext设置为某个地方的当前通道吗?无论如何,我在代码中看不到这一点,而且网格也没有命名,所以请确保(调试器)正确设置了DataContext。我还同意CodingBarfield的说法,即您以一种非常奇怪的方式构建代码,这可能是我们尚未找到修复方法的原因。@Akku:是的,我收到错误是因为Grid.Resources。此电压等级是用户控件,而不是窗口。还有别的办法吗?另外,我已经更新了xaml.cs类:)我如何才能实现此可用性?无需定义BooleantVisibilityConverter,您可以使用默认实现@Patrick:Hey bro它会在我的xaml文件上引发异常<代码>“在”System.Windows.StaticResourceExtension“上提供值”引发了异常。我已更新了xaml。检查是否正确?纠正了问题:)谢谢你的帮助,伙计:)