【问题标题】:How can i bind data in ListViewItem?如何在 ListViewItem 中绑定数据?
【发布时间】:2018-12-21 07:11:57
【问题描述】:

我有一个带有一些列的 ListView。其中一个列包含两个 RadioButton。当我检查 RadioButton 时,我想更改最后一列的绑定值(它的标题名称是 Fee)。我不知道,有人可以帮我吗?

<ListView x:Name="lvTimeView">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="ID" Width="175" DisplayMemberBinding="{Binding serverId}"></GridViewColumn>
                <GridViewColumn Header="IP" Width="175" DisplayMemberBinding="{Binding serverIp}"></GridViewColumn>
                <GridViewColumn x:Name="gvcTime" Header="Time" Width="400">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="30">
                                <RadioButton x:Name="rbOneMonth" Tag="1" Checked="rbChecked" Style="{StaticResource BoxRadioButton}" HorizontalAlignment="Left">OneMonth</RadioButton>
                                <RadioButton x:Name="rbTwoMonth" Tag="2" Margin="8,0,0,0" Checked="rbChecked" Style="{StaticResource BoxRadioButton}" HorizontalAlignment="Left">TwoMonth</RadioButton>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Fee" Width="100" DisplayMemberBinding="{Binding fee}">
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

【问题讨论】:

    标签: wpf listview binding


    【解决方案1】:

    首先,忘记这个例子中的事件。这是利用 WPF 绑定的完美示例...

    将您的 XAML 更改为此。

    <ListView ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="ID" Width="175" DisplayMemberBinding="{Binding ServerId}"></GridViewColumn>
                    <GridViewColumn Header="IP" Width="175" DisplayMemberBinding="{Binding ServerIp}"></GridViewColumn>
                    <GridViewColumn Header="Time">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <RadioButton GroupName="{Binding ServerId}" IsChecked="{Binding OneMonth}">OneMonth</RadioButton>
                                    <RadioButton GroupName="{Binding ServerId}" IsChecked="{Binding TwoMonth}" Margin="5,0,0,0">TwoMonth</RadioButton>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Fee" Width="100" DisplayMemberBinding="{Binding Fee}">
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
    

    注意 RadioButtons GroupName 属性。这只是为了让单选按钮知道它们是单选按钮组的一部分,因此当您选择一个时,另一个取消选择。我们必须将其设置为每行唯一的值,所以我选择了 serverId。我希望这在您的情况下是独一无二的,否则您将不得不生成一些 Guid 或其他东西......

    接下来是模型...

    public class Item : INotifyPropertyChanged
    {
        private string _serverId;
        private string _serverIp;
        private bool _oneMonth;
        private bool _twoMonth;
        private decimal _fee;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public string ServerId
        {
            get => _serverId;
            set
            {
                _serverId = value;
                OnPropertyChanged(nameof(ServerId));
            }
        }
        public string ServerIp
        {
            get => _serverIp;
            set
            {
                _serverIp = value;
                OnPropertyChanged(nameof(ServerIp));
            }
        }
        public bool OneMonth
        {
            get => _oneMonth;
            set
            {
                _oneMonth = value;
                if(value)
                    Fee = 100;
                OnPropertyChanged(nameof(OneMonth));
            }
        }
        public bool TwoMonth
        {
            get => _twoMonth;
            set
            {
                _twoMonth = value;
                if(value)
                    Fee = 200;
                OnPropertyChanged(nameof(TwoMonth));
            }
        }
        public decimal Fee
        {
            get => _fee;
            set
            {
                _fee = value;
                OnPropertyChanged(nameof(Fee));
            }
        }
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    实例化模型并将其添加到数据上下文中...

    public partial class MainWindow : Window
    {
        public IEnumerable<Item> Items => new Item[]
        {
            new Item { ServerId = "asia", ServerIp = "10.1.1.45" },
            new Item { ServerId = "europe", ServerIp = "45.1.4.211" },
            new Item { ServerId = "australia", ServerIp = "37.2.1.181" }
        };
    
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = this;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多