【问题标题】:WPF-MVVM Radio Button generated automatically自动生成的 WPF-MVVM 单选按钮
【发布时间】:2023-04-01 17:19:01
【问题描述】:

我有一个提交表单的应用程序。该表单包含多个单选按钮,它们彼此相邻显示 1。我需要从列表中加载颜色并为每种颜色创建一个单选按钮,然后当用户选择一种颜色时,我想从控件中获取“SelectedItem”。我知道您可以通过列表轻松做到这一点,但是当我需要将控件彼此相邻放置时,我该怎么做??

代码:

<Grid>
    <StackPanel VerticalAlignment="Top">
        <GroupBox Name="CheckBoxes" Margin="5" >
            <StackPanel Name="wrpCheckBoxes" DataContext="{Binding ListParts}">
                <RadioButton Name="chkRed" Content="{Binding Description}" Visibility="{Binding DataBindingModel.ColorRed}" Tag="{Binding ID}" />
                <RadioButton Name="chkGreen" Content="Green" Visibility="{Binding DataBindingModel.ColorGreen}" />
                <RadioButton Name="chkBlue" Content="Blue" Visibility="{Binding DataBindingModel.ColorBlue}" />
                <RadioButton Name="chkGray" Content="Gray" Visibility="{Binding DataBindingModel.ColorGray}" />
                <RadioButton Name="chkYellow" Content="Yellow" Visibility="{Binding DataBindingModel.ColorYellow}" />
                <RadioButton Name="chkBlack" Content="Black" Visibility="{Binding DataBindingModel.ColorBlack}" />
            </StackPanel>
        </GroupBox>
    </StackPanel>

    <StackPanel VerticalAlignment="Bottom" Margin="5">
        <ListView x:Name="listBoxItems" BorderThickness="1" ItemsSource="{Binding ListParts}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Index" DisplayMemberBinding="{Binding Index}" />
                    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" />
                </GridView>
            </ListView.View>
        </ListView>

        <!--<ListBox x:Name="listBoxItems" ItemsSource="{Binding ListParts}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <RadioButton GroupName="rbList" Tag="{Binding}" Content="{Binding Description}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>-->
    </StackPanel>
</Grid>

备注:ListParts 只是一个包含颜色的列表,这个视图也附加到一个视图模型

视图模型:

public class DataBindingViewModel : ViewModelBase
{
    #region Private Fields
    private DataBindingModel _DataBindingModel;
    private List<PartsModel> _ListParts;

    private string _Description1;
    private int _TagID;
    #endregion

    #region Properties
    public DataBindingModel DataBindingModel
    {
        get { return this._DataBindingModel; }
        set
        {
            if (this._DataBindingModel == value)
                return;

            this._DataBindingModel = value;
            OnPropertyChanged("DataBindingModel");
        }
    }

    public List<PartsModel> ListParts
    {
        get { return this._ListParts; }
        set
        {
            if (this._ListParts == value)
                return;

            this._ListParts = value;
            OnPropertyChanged("ListParts");
        }
    }

    public string Description1
    {
        get { return this._Description1; }
        set
        {
            if (this._Description1 == value)
                return;

            this._Description1 = value;
            OnPropertyChanged("Description1");
        }
    }

    public int TagID
    {
        get { return this._TagID; }
        set
        {
            if (this._TagID == value)
                return;

            this._TagID = value;
            OnPropertyChanged("TagID");
        }
    }
    #endregion

    public DataBindingViewModel(DataBindingModel DataBinding)
    {
        this.DataBindingModel = DataBinding;
        this.ListParts = Common.GetData();


    }
}

Common Class 只是在加载数据:

public static class Common
{
    public static List<PartsModel> GetData()
    {
        List<PartsModel> listParts = new List<PartsModel>();

        listParts.Add(new PartsModel(1, "1234561", "Color Red", Convert.ToDecimal(15.99)));
        listParts.Add(new PartsModel(2, "1234562", "Color Green", Convert.ToDecimal(17.00)));
        listParts.Add(new PartsModel(3, "1234563", "Color Blue", Convert.ToDecimal(12.95)));
        listParts.Add(new PartsModel(4, "1234564", "Color Gray", Convert.ToDecimal(9.95)));
        listParts.Add(new PartsModel(5, "1234565", "Color Yellow", Convert.ToDecimal(10.55)));
        listParts.Add(new PartsModel(6, "1234566", "Color Black", Convert.ToDecimal(99.99)));

        return listParts;
    }
}

如何在不使用 ListView 的情况下在每个单选按钮上显示 ListParts 的成员?

如果你们需要更多信息,请告诉我,感谢您的回答

【问题讨论】:

    标签: c# wpf list mvvm radio-button


    【解决方案1】:

    您可以使用ListBox.ItemsPanel 指定您想要的面板类型 - 可能是&lt;StackPanel Orientation="Horizontal/&gt;

    <ListBox>
      <ListBox.ItemTemplate>
        <DataTemplate>
          <RadioButton GroupName="rbList" Tag="{Binding}" Content="{Binding Description}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      <ListBox.ItemsPanel>
    <ListBox>
    

    更多信息请参见the examples in the ItemsPanel property documentation on MSDN

    【讨论】:

    • 谢谢你的回答 NextlnLine 我试过这个&lt;ListBox x:Name="listBoxItems" ItemsSource="{Binding ListParts}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;RadioButton GroupName="rbList" Tag="{Binding}" Content="{Binding Description}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; 但它不是水平的,它总是每个颜色 1 行
    • 它向我展示了每行 1 个颜色项,我需要它是每行多个颜色项 例如:RadioButton1 RadioButton2 RadioButton3 ... etc Not RadioButton1 /n RadioButton2 /n RadioButton3 /n
    • 是的,这就是为什么除了ItemTemplate 之外,您还必须自定义ItemsPanelItemsPanel 控制ListBox(或任何其他ItemsControl)使用哪种面板来显示其项目。
    • 你能给我举个例子吗,我很难想象它。非常感谢!
    • 我编辑了原始答案以添加一个示例 - 只需重新加载页面,它就会出现。
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 2014-04-25
    • 2014-06-19
    • 2010-10-27
    • 2013-08-13
    • 2015-12-19
    • 2011-02-12
    • 2018-06-18
    相关资源
    最近更新 更多