gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:44:31 26 4
gpt4 key购买 nike

Pager.xaml(查看)

<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(后面的代码)
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; }
}
...
}
}

PagerViewModel.cs
namespace Client.ViewModel
{
public class PagerViewModel : ViewModelBase
{
...

IPagableEntry _selectedEntry;
public IPagableEntry SelectedEntry
{
get
{
return _selectedEntry;
}

set
{
_selectedEntry = value;
OnPropertyChanged("SelectedEntry");
}
}
...
}
}

MainPage.xaml(查看)
<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>

MainPageViewModel.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 分配绑定(bind)?这告诉我们 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;
}
}

关于c# - 我无法获取自定义控件的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33078558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com