如何在ListViewItem中绑定数据?

时间:2018-12-21 07:11:57

标签: wpf listview binding

我有一个带有某些列的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>

1 个答案:

答案 0 :(得分:0)

首先,请忽略本示例中的事件。这是利用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;
    }
}
相关问题