Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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/1/vue.js/6.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#_Wpf_Data Binding_Event Handling - Fatal编程技术网

C# 如何访问按钮的绑定值?

C# 如何访问按钮的绑定值?,c#,wpf,data-binding,event-handling,C#,Wpf,Data Binding,Event Handling,我有以下XAML <Window.Resources> <DataTemplate x:Key="SelectButtonColumnDataTemplate"> <Button Content="Select" Command="{Binding SelectItemCommand}" Click="SelectButtonClick" /> </DataTemplate> </Window.Resource

我有以下XAML

<Window.Resources>
    <DataTemplate x:Key="SelectButtonColumnDataTemplate">
        <Button Content="Select" Command="{Binding SelectItemCommand}" Click="SelectButtonClick" />
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<ListView x:Name="listFeedSearch" HorizontalAlignment="Left" Width="542.5" RenderTransformOrigin="0.5,0.5" ItemsSource="{Binding SearchCollection}">
    <ListView.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>                
            <RotateTransform/>                
            <TranslateTransform/>            
            </TransformGroup>        
            </ListView.RenderTransform>        
    <ListView.View>            
        <GridView>                
            <GridViewColumn Width="150" Header="Feed Name" x:Name="listFeedSearchName" DisplayMemberBinding="{Binding FeedName}"  />                
            <GridViewColumn Width="280" Header="Feed Address" x:Name="listFeedSearchAddress" DisplayMemberBinding="{Binding FeedUrl}" />                
            <GridViewColumn Width="100" Header=" " CellTemplate="{StaticResource SelectButtonColumnDataTemplate}" />            
        </GridView>        
        </ListView.View>    
</ListView>     
</Grid>
我想做的是输出单个绑定值的行

我该怎么做,我知道这是一个大致的过程:

var data = (sender as Button)...

谢谢

如果我理解正确,您需要使用
{Binding}


参考:

如果将button CommandParameter设置为(感谢Bolu):

然后,下面的代码将检索当前单击的对象并显示messagebox。其中,foo是用于填充gridview的类

 public class foo
 {
    string FeedName {get;set;}
    string FeedUrl {get;set;}
 }

 public void InitialiseListView(List<Foo> Items)
 {
    listFeedSearch.ItemsSource = Items;
 }

 private void SelectButtonClick(object sender, RoutedEventArgs e)
 {
    foo b = (foo)(sender as Button).CommandParameter;

    string s = string.format("Name: {0} URL: {1}",b.FeedName,b.FeedUrl);
    MessageBox.Show(s);
}
公共类foo
{
字符串FeedName{get;set;}
字符串FeedUrl{get;set;}
}
public void InitialiseListView(列表项)
{
listFeedSearch.ItemsSource=项目;
}
私有无效选择按钮单击(对象发送方,路由目标)
{
foob=(foo)(发送方作为按钮).CommandParameter;
string s=string.format(“Name:{0}URL:{1}”,b.FeedName,b.FeedUrl);
MessageBox.Show(s);
}

foo是数据来源的类的名称,例如我有一个类。在我的示例中,列表的itemsource被设置为“list”,我将编辑我的帖子以更好地显示这一点
"{Binding}"
 public class foo
 {
    string FeedName {get;set;}
    string FeedUrl {get;set;}
 }

 public void InitialiseListView(List<Foo> Items)
 {
    listFeedSearch.ItemsSource = Items;
 }

 private void SelectButtonClick(object sender, RoutedEventArgs e)
 {
    foo b = (foo)(sender as Button).CommandParameter;

    string s = string.format("Name: {0} URL: {1}",b.FeedName,b.FeedUrl);
    MessageBox.Show(s);
}