我无法获取自定义控件的选定项

本文关键字:自定义控件 获取 | 更新日期: 2023-09-27 18:17:26

Pager.xaml(View(

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="Client.View.Pager">
    ...
    <ListBox x:Name="listBoxEntries"
             ItemsSource="{Binding Path=ListCollectionView}"
             BorderThickness="0"
             Margin="0"
             Style="{StaticResource common}"
             HorizontalContentAlignment="Stretch"
             VerticalContentAlignment="Stretch"
             ItemTemplate="{StaticResource templateTableCategory}"
             SelectedItem="{Binding Path=SelectedEntry, Mode=TwoWay}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="{Binding Path=Rows}"
                             Columns="{Binding Path=Columns}"
                             IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    ...
</Grid>

Pager.xaml.cs(Behind code(

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Client.Model;
using Client.ViewModel;
namespace Client.View
{
    public partial class Pager
    {
        public static readonly DependencyProperty SelectedEntryProperty = DependencyProperty.Register("SelectedEntry", typeof(IPagableEntry), typeof(Pager));
        ...
        public IPagableEntry SelectedEntry
        {
            get { return (DataContext as PagerViewModel).SelectedEntry; }
            set { (DataContext as PagerViewModel).SelectedEntry = value; }
        }
        ...
    }
}

寻呼机视图模型.cs

namespace Client.ViewModel
{
    public class PagerViewModel : ViewModelBase
    {
        ...
        IPagableEntry _selectedEntry;
        public IPagableEntry SelectedEntry
        {
            get
            {
                return _selectedEntry;
            }
            set
            {
                _selectedEntry = value;
                OnPropertyChanged("SelectedEntry");
            }
        }
        ...
    }
}

MainPage.xaml(View(

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="Client.View.MainPage"
      Style="{StaticResource common}">
    <Page.DataContext>
        <viewModel:MainPageViewModel/>
    </Page.DataContext>
    ...
    <view:Pager x:Name="pagerTableCategories"
            Grid.Row="0"
            List="{Binding Path=PagerTableCategoriesItems}"
            Rows="{Binding Path=PagerTableCategoriesRows}"
            Columns="{Binding Path=PagerTableCategoriesColumns}"
            SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
    </view:Pager>
    ...
</Page>

主页视图模型.cs

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
    public class MainPageViewModel : ViewModelBase
    {
        ...
        IPagableEntry _selectedTableCategory;
        public IPagableEntry SelectedTableCategory
        {
            get
            {
                return _selectedTableCategory;
            }
            set
            {
                _selectedTableCategory = value;
                MessageBox.Show("Got it!");
            }
        }
        ...
    }
}

我制作了一个自定义面板"寻呼机",那就是ViewModel。我想在我的主页中显示寻呼机。我希望我选择一个项目,然后 MainPageViewModel 的属性 SelectTableCategory 将更改,并且显示一个带有字符串"知道了!但它不起作用...我的问题是什么?

附言。我的英语不是很好。感谢您的理解。

我无法获取自定义控件的选定项

让我们从第一原则开始工作。

您有一个名为 Pager 的自定义控件,它将像这样使用

<view:Pager x:Name="pagerTableCategories"
        SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
</view:Pager>

请参阅如何为 SelectedEntry 分配绑定?这告诉我们 Pager 必须将 SelectedEntry 实现为依赖项属性。我们如何做到这一点?

public class Pager
{
    // Using a DependencyProperty as the backing store for PrePend.  
    //   This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedEntryProperty =
        DependencyProperty.Register("SelectedEntry", typeof(object), 
          typeof(Pager), 
          new PropertyMetadata(null, OnSelectedEntryChanged));
    public object SelectedEntry
    {
         get { return GetValue(SelectedEntryProperty); }
         set { SetValue(SelectedEntryProperty, value); }
    }
    private static void OnSelectedEntryChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e) 
    {
         // TODO set the SelectedItem in the ListBox
    }
}

接下来,您需要显示寻呼机。你的 XAML 看起来正常。应在 ListBox 上添加一个事件处理程序,以侦听所选内容已更改的事件。这样,您可以在发生这种情况时更新SelectedEntry

public class Pager
{
    // continued...
    // *Updated*
    private listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.SelectedEntry = (sender as ListBox).SelectedItem;
    }
}