Lightstar
Lightstar

Reputation: 193

wpf Button in listview with binding

I decleared a type of class for collection

public class inoutboundpd
{
    public string srcip { get; set; }
    public string srcport { get; set; }
    public string dstip { get; set; }
    public string dstport { get; set; }
    public string protocol { get; set; }
    public Button dpibutton { get; set; }
    public Button delbutton { get; set; }
}
ObservableCollection<inoutboundpd> _inboundp = new ObservableCollection<inoutboundpd>();
public ObservableCollection<inoutboundpd> inboundp
{
    get { return _inboundp; }
}  //*/

And this is my not completed source of xaml

<ListView x:Name="policylistScrollViewer" ItemsSource="{Binding inboundp}"  HorizontalAlignment="Left" Height="169" VerticalAlignment="Bottom" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="575">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Source IP" Width="110" DisplayMemberBinding="{Binding Path=srcip}" />
            <GridViewColumn Header="Protocol" Width="50" DisplayMemberBinding="{Binding Path=protocol}" />
            GridViewColumn Header="DPI" Width="40" >
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="DPI" Command="{Binding Path=dpibutton}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
            <GridViewColumn Header="Delete" Width="40" CellTemplate="{DynamicResource delbutton}">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

The two button are not work for my intend. And

Button bt = new Button();
bt.Content = "DPI";
bt.Tag = url + " " + image + " " + audio + " " + video + " " + word;
//bt.PreviewMouseDown += new MouseButtonEventHandler(dpiconfirm);
_inboundp.Add(new inoutboundpd { srcip = isip, srcport = isport, dstip = idip, dstport=idport , protocol=protocol, dpibutton=bt, delbutton=dbt });

This not work. How can i binding the bt Button to listview button column? I want to add the button to listview column dynamically with the event 'dpiconfirm' and 'Button.Tag etc...

Upvotes: 1

Views: 556

Answers (1)

helb
helb

Reputation: 7793

Here's a summary of recommendatory comments which lead to a working solution:

dpibutton should be an ICommand, not a Button. See ButtonBase.Command

public class inoutboundpd 
{
    public string srcip { get; set; }
    public string srcport { get; set; }
    public string dstip { get; set; }
    public string dstport { get; set; }
    public string protocol { get; set; }
    public ICommand dpibutton { get; set; }
    public ICommand delbutton { get; set; }
}

Then you must set the ICommand properties before the view is created. For this, you must create classes which implement ICommand. Make sure the CanExecute() implementation returns true.

I also recommand calling a common private function from MouseButtonEventHandler and ICommand.Execute to handle the button click.

Upvotes: 1

Related Questions