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!");
}
}
...
}
}
我做了一個自定義面板 '尋呼機',這就是視圖模型。 ,我想在我的MainPage中顯示尋呼機。 我期望我選擇一個項目,然後MainPageViewModel的屬性SelectTableCategory將會改變,並且一個消息框顯示爲字符串「Got it!」 但它不起作用... 我的問題是什麼?
ps。我不擅長英語。 我希望你的理解。
您的'SelectedEntry'屬性應該知道關於您的View Model的** nothing **。因此,使用'DataContext'通過依賴項屬性返回視圖模型中的屬性當然不是實現此目的的最佳方法。自定義依賴項屬性不需要依賴作爲特定視圖模型類型的'DataContext'。 **編輯**標準的依賴屬性應該可以解決這個問題。只需輸入'propdp' - > Tab - > Tab,Visual Studio會爲您提供一個。 –