Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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/9/csharp-4.0/2.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#_Itemssource - Fatal编程技术网

C# 如何从控件中弹出类字段?

C# 如何从控件中弹出类字段?,c#,itemssource,C#,Itemssource,我有自己的用户控制。此控件的代码: <StackPanel> <TextBlock Text="{Binding Login}" Margin="0,18,0,0" HorizontalAlignment="Center" FontWeight="Bold"/> <TextBlock Text="{Binding Address}" HorizontalAlignment="Center" /> </StackPanel> <

我有自己的用户控制。此控件的代码:

<StackPanel>
    <TextBlock Text="{Binding Login}" Margin="0,18,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
    <TextBlock Text="{Binding Address}" HorizontalAlignment="Center" />
</StackPanel>
</Border>
<Button Name="watchBut" Grid.Row="1" Style="{StaticResource RoundButtonTemplate}" Margin="5,0,5,5" FontSize="15" Content="Watch" Click="watchBut_Click"/>
其填充方式:

List<MyItem> Computers = new List<MyItem>
{
    new MyItem(0,"08739","10.3.0.9"),
    new MyItem(1,"08813","10.3.0.11"),
    new MyItem(2,"09832","10.3.0.14"),
    new MyItem(3,"09854","10.3.0.12"),
    new MyItem(4,"09984","10.3.0.17"),
    new MyItem(5,"proskurin","10.3.0.1"),
    new MyItem(6,"karavaev","10.3.0.2"),
    new MyItem(7,"deba","10.3.0.13")
};
items.ItemsSource = Computers;
List计算机=新列表
{
新的MyItem(0,“08739”,“10.3.0.9”),
新的MyItem(1,“08813”,“10.3.0.11”),
新的MyItem(2,“09832”,“10.3.0.14”),
新的MyItem(3,“09854”,“10.3.0.12”),
新的MyItem(4,“09984”,“10.3.0.17”),
新的MyItem(5,“proskurin”,“10.3.0.1”),
新的MyItem(6,“卡拉瓦耶夫”,“10.3.0.2”),
新的MyItem(7,“deba”,“10.3.0.13”)
};
items.ItemsSource=计算机;
我想通过单击此矩形下的“Watch”按钮来获取MyItem类对象信息(例如,“09984”、“10.3.0.17”)。大概是这样的:

void watchBut_Click(object sender, RoutedEventArgs e)
{
    var myItem = ((Button)sender).DataContext as MyItem;

    //  Do stuff
}
您可以改为将datacontext作为命令参数发送:

<Button 
    Name="watchBut" 
    Grid.Row="1" 
    Style="{StaticResource RoundButtonTemplate}" 
    Margin="5,0,5,5" 
    FontSize="15" 
    Content="Watch" 
    Command="{Binding WatchCommand}"
    CommandParameter="{Binding}"
    />


没有路径的绑定只是将DataContext本身(本例中为
MyItem
的实例)绑定到属性。这是一个“更好”的、更纯粹的MVVM解决方案,但您在这里做的不是非常纯粹的MVVM,事件处理程序也不是致命的罪过

“我想获取MyItem类对象信息”--在何处获取并使用它做什么?什么按钮?什么长方形?“弹出”对您意味着什么?我想在代码(.cs)中包含它。打开屏幕截图。有一个控制网格。一个控件包含两个文本块(08739,10.3.0.9)的灰色矩形和一个按钮,按钮下方有文本“Watch”。我有一个名为Computers的MyItem类(字符串登录,字符串地址)对象列表。(“items”是ItemsControl的名称):items.ItemsSource=计算机;那么,通过单击按钮,我想获取MyItem类objectWhat按钮?什么长方形?“弹出”对你意味着什么?我并不是要你重复你在问题中已经说过的每一句话。“注意”按钮。灰色矩形,包含两个字符串。这对我来说意味着从UserControl@ShaamaanOP没有绑定到ItemsSource,但他有一个绑定到
MyItem
属性的DataTemplate。
void watchBut_Click(object sender, RoutedEventArgs e)
{
    var myItem = ((Button)sender).DataContext as MyItem;

    //  Do stuff
}
<Button 
    Name="watchBut" 
    Grid.Row="1" 
    Style="{StaticResource RoundButtonTemplate}" 
    Margin="5,0,5,5" 
    FontSize="15" 
    Content="Watch" 
    Command="{Binding WatchCommand}"
    CommandParameter="{Binding}"
    />