Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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# 从Listboxitem模板中的标签获取值_C#_Wpf_Controltemplate - Fatal编程技术网

C# 从Listboxitem模板中的标签获取值

C# 从Listboxitem模板中的标签获取值,c#,wpf,controltemplate,C#,Wpf,Controltemplate,我的问题是我需要模板中标签的值作为字符串。它是带有复选框和标签的列表框项目的模板。选中该框后,我需要标签的值作为代码中的字符串变量 ListBox <ListBox HorizontalAlignment="Left" Height="196" Margin="10,24,0,0" VerticalAlignment="Top" Width="155" BorderBrush="#FF7799BB" Name="ListBoxTblC

我的问题是我需要
模板中
标签的
值作为字符串。它是带有
复选框和
标签的
列表框项目的
模板。选中该框后,我需要
标签的
作为代码中的字符串变量

ListBox

<ListBox HorizontalAlignment="Left" Height="196" Margin="10,24,0,0" 
             VerticalAlignment="Top" Width="155" BorderBrush="#FF7799BB" 
             Name="ListBoxTblColumns" DataContextChanged="TableColumns_DataContextChanged" 
             ItemsSource="{Binding Columns}">

    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Name="columnSelectBox" Margin="0,7,4,0" Checked="columnSelectBox_Checked" Unchecked="columnSelectBox_Unchecked"/>
                <Label Name="lblColName" Content="{Binding}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

想法?

由于
标签
只是绑定到DataContext,而且
复选框
具有相同的DataContext,您可以使用
(发送者作为复选框)。DataContext.ToString()
。谢谢您的快速回答。我实际上不明白您想要做什么,您能给我举个例子吗?因为,我想这只能通过绑定来完成。
public void columnSelectBox_Checked(object sender, RoutedEventArgs e)
{  
    string colName = ...;
}