【问题标题】:how to access the label inside datatemplate如何访问数据模板中的标签
【发布时间】:2013-01-24 12:41:52
【问题描述】:

大家好,我有一个列表框,其中有一个数据模板。里面是复选框、文本框、标签...我想要的是在未选中复选框时获取标签的值?或关于如何访问标签值的任何替代方法,但只有未选中复选框............请帮帮我。 代码如下

<ListBox.ItemTemplate>                                                    
    <DataTemplate>
        <StackPanel Name="sp" Orientation="Horizontal" Margin="3,3,3,3" >                                                       
            <CheckBox Name="chkSubject"   IsChecked="{Binding RelativeSource{RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" VerticalAlignment="Center"  Margin="0,0,4,0" Unchecked="chkSubject_Unchecked">
                <TextBlock FontSize="11" Text="{Binding subject_name}" />
            </CheckBox>
            <Label Name="lbl_idOfSub" Content="{Binding subject_id}" Visibility="Visible">
            </Label>
        </StackPanel>
     </DataTemplate>
</ListBox.ItemTemplate>

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    由于您在标签上使用绑定,我会从数据模板描述的对象中访问 subject_id。像这样:

    var subjectId = dataBoundItem.subject_id;
    

    这是使用 MVVM 和绑定的正确方法。

    更新:

    这是解决此问题的基本 MVVM 方法。首先,我清理了一些列表框声明并添加了一个设置 IsSelected 绑定的触发器:

        <ListBox ItemsSource="{Binding}">
            <ListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                </Style>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="sp" Orientation="Horizontal" Margin="3,3,3,3" >
                        <CheckBox Name="chkSubject"   IsChecked="{Binding IsSelected}" VerticalAlignment="Center"  Margin="0,0,4,0" Unchecked="chkSubject_Unchecked_1">
                            <TextBlock FontSize="11" Text="{Binding SubjectName}" />
                        </CheckBox>
                        <Label Name="lbl_idOfSub" Content="{Binding SubjectId}" Visibility="Visible"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    在这里,只要单个 ListBoxItem 上的 IsSelected 值发生更改,viewModel 的“IsSelected”绑定就会更改。这是模型:

    public class SelectableItem : INotifyPropertyChanged
    {
        private string _subjectId;
        private bool _isSelected;
        private string _subjectName;
    
        public string SubjectId
        {
            get { return _subjectId; }
            set { _subjectId = value; OnPropertyChanged("SubjectId"); }
        }
    
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; OnPropertyChanged("IsSelected"); }
        }
    
        public string SubjectName
        {
            get { return _subjectName; }
            set { _subjectName = value; OnPropertyChanged("SubjectName"); }
        }
        // .. INotifyPropertyChangedImplementation
    

    您的 IsSelected 将在相关项目被选中时设置为 true,而在未选中时设置为 false。您可以将您的代码放入“IsSelected”属性的“set”项并检查(值 == false)并执行您认为合适的必要代码段。这将是解决问题的 MVVM 方法。

    使用该事件,您可以执行以下操作:

        private void chkSubject_Unchecked_1(object sender, RoutedEventArgs e)
        {
            FrameworkElement control = sender as FrameworkElement;
            if (control == null)
                return;
    
            SelectableItem item = control.DataContext as SelectableItem;
            if (item == null)
                return;
    
            string yourValue = item.SubjectId;
        }
    

    我强烈建议您阅读有关 MVVM 和绑定的信息。

    【讨论】:

    • 谢谢。顺便说一句,你的意思是 dataBoundItem。你能不能给我的问题一个可行的解决方案,因为我被困在这一点上。我想要的是当复选框未被选中时,我想要标签中的值。
    • 感谢 Dmitriy 的帮助,它非常有用....一定会读到它......:)
    【解决方案2】:

    如何使用 CheckBox 的 CheckedUnChecked 事件,以便您可以检索绑定到标签的 subject_id 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-10
      • 2012-03-25
      • 2014-08-01
      • 1970-01-01
      • 2012-01-24
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多