C# 我无法获取自定义控件的选定项
Pager.xamlViewC# 我无法获取自定义控件的选定项,c#,wpf,xaml,mvvm,binding,C#,Wpf,Xaml,Mvvm,Binding,Pager.xamlView <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"
<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>
PagerViewModel.cs
namespace Client.ViewModel
{
public class PagerViewModel : ViewModelBase
{
...
IPagableEntry _selectedEntry;
public IPagableEntry SelectedEntry
{
get
{
return _selectedEntry;
}
set
{
_selectedEntry = value;
OnPropertyChanged("SelectedEntry");
}
}
...
}
}
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!");
}
}
...
}
}
MainPage.xamlView
<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>
我做了一个自定义面板“寻呼机”,这就是ViewModel。
我想在我的主页上显示寻呼机。
我希望我选择一个项目,然后MainPageViewModel的属性SelectTableCategory将发生更改,并显示一个消息框,其中包含字符串Got!
但它不起作用。。。
我有什么问题
另外,我的英语不是很好。
如果您能理解,我将不胜感激。让我们从第一原则出发 您有一个名为Pager的自定义控件,它将像这样使用
<view:Pager x:Name="pagerTableCategories"
SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
</view:Pager>
接下来,您需要显示寻呼机。你的XAML看起来不错。您应该在ListBox上添加一个事件处理程序来侦听选择更改事件。这样,您可以在出现这种情况时更新SelectedEntry
public class Pager
{
// continued...
// *Updated*
private listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.SelectedEntry = (sender as ListBox).SelectedItem;
}
}
让我们从第一原则出发 您有一个名为Pager的自定义控件,它将像这样使用
<view:Pager x:Name="pagerTableCategories"
SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
</view:Pager>
接下来,您需要显示寻呼机。你的XAML看起来不错。您应该在ListBox上添加一个事件处理程序来侦听选择更改事件。这样,您可以在出现这种情况时更新SelectedEntry
public class Pager
{
// continued...
// *Updated*
private listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.SelectedEntry = (sender as ListBox).SelectedItem;
}
}
SelectedEntry属性不应了解视图模型。因此,使用DataContext通过dependency属性在视图模型中返回属性肯定不是实现这一点的最佳方法。自定义依赖项属性不需要依赖于作为特定视图模型类型的DataContext。编辑标准依赖项属性可以解决此问题。只需键入propdp->Tab->Tab,visual studio就会为您提供一个。您的SelectedEntry属性应该对您的视图模型一无所知。因此,使用DataContext通过dependency属性在视图模型中返回属性肯定不是实现这一点的最佳方法。自定义依赖项属性不需要依赖于作为特定视图模型类型的DataContext。编辑标准依赖项属性可以解决此问题。只需键入propdp->Tab->Tab,visual studio就会为您提供一个。非常感谢!但是,如何设置列表框中的项目?该方法SelectedEntryChanged是静态的,但listbox不是静态的,因此我无法在静态方法中访问listbox;但MainPageViewModel中SelectedTableCategory的set属性尚未自动调用…修复了listBox中的错误\u SelectionChangedHanks!但是,如何设置列表框中的项目?该方法SelectedEntryChanged是静态的,但listbox不是静态的,因此我无法在静态方法中访问listbox;但MainPageViewModel中SelectedTableCategory的set属性尚未自动调用…修复了listBox中的错误\u SelectionChanged