您好我正在嘗試創建一個自定義用戶控制元件,其中包含一個 ListView,其中包含一個如下所示的資料模板:
<ComboBox ItemsSource="{Binding ItemsSource, ElementName=root}"
SelectedItem="{Binding SelectedItem, ElementName=root, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- This is what I tried:
Working but not what I want <TextBox Text="{Binding Name}"/>
Returns the List only the word "FallBack" <TextBox Text="{Binding ItemText, ElementName=root}" />
Returns the LIst empty <TextBox Text="{Binding ItemText}" />
-->
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在后面的代碼中,我為這種情況創建了必要的依賴屬性(所以我假設)唯一相關的是關于專案文本,它看起來像這樣:
#region ItemText
public string ItemText
{
get { return (string)GetValue(ItemTextProperty); }
set { SetValue(ItemTextProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemTextProperty =
DependencyProperty.Register("ItemText", typeof(string), typeof(CardComboBox), new PropertyMetadata("FallBack"));
#endregion
我想做的是像這樣添加用戶控制元件
<local:CardComboBox ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson}" ItemText="{Binding Name}" IsEnabled="True" />
選項一:
<TextBox Text="{Binding Name}"/>
作業正常,因為類人的 PropertyName 是名稱,但我顯然不想硬編碼它。我想將它系結到我喜歡的任何屬性。
選項 2:
<TextBox Text="{Binding ItemText, ElementName=root}" />
給我 7 個專案的串列(根據串列),但由于 DependencyPropertyMetadata 僅顯示單詞“Fallback”。
選項 3:
<TextBox Text="{Binding ItemText}" />
給我清單,但根本沒有文字。
我也嘗試使用 relativeSource,但結果相似。
#region Person
Person _selectedPerson;
public Person SelectedPerson
{
get => _selectedPerson;
set
{
if (value != _selectedPerson)
{
_selectedPerson = value;
OnPropertyChanged("SelectedPerson");
}
}
}
ObservableCollection<Person> _persons;
public ObservableCollection<Person> Persons
{
get => _persons;
set
{
if (value != _persons)
{
_persons = value;
OnPropertyChanged("Persons");
}
}
}
public void populatePersons()
{
Persons = new ObservableCollection<Person>();
Persons.Add(new Person("Carl"));
Persons.Add(new Person("Max"));
Persons.Add(new Person("May"));
Persons.Add(new Person("Jen"));
Persons.Add(new Person("Charly"));
Persons.Add(new Person("Nora"));
Persons.Add(new Person("Yvonne"));
}
#endregion
我已經添加了我系結的串列。在 ViewModel 的建構式中呼叫方法 Populate Persons
uj5u.com熱心網友回復:
<TextBox Text="{Binding ItemText, RelativeSource={RelativeSource AncestorType=local:CardComboBox}}" />如果這是你想要的,將系結到父依賴屬性TextBox的當前值。ItemTextCardComboBox
但是,如果要顯示Name每個Person位元組的屬性值,還希望能夠Person使用您的屬性指定要系結到的屬性的名稱ItemText,則必須以編程方式創建系結。
uj5u.com熱心網友回復:
到目前為止,最接近的解決方案是公開 ItemContentTemplate 依賴屬性,然后將其系結到靜態資源(例如來自 App.xaml)
UserControl 的 XAML 如下所示:
<StackPanel Style="{StaticResource CardStackPanel}" Orientation="{Binding Orientation, ElementName=root}" >
<Label x:Name="Label" Content="{Binding TitleText, ElementName=root}"/>
<ComboBox ItemsSource ="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
ItemTemplate="{Binding ItemContentTemplate, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=TwoWay}" />
</StackPanel>
ComboBox 部分背后的代碼:
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CardComboBox));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(CardComboBox));
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public DataTemplate ItemContentTemplate
{
get { return (DataTemplate)GetValue(ItemContentTemplateProperty); }
set { SetValue(ItemContentTemplateProperty, value); }
}
public static readonly DependencyProperty ItemContentTemplateProperty =
DependencyProperty.Register("ItemContentTemplate", typeof(DataTemplate), typeof(CardComboBox));
App.xaml 中的靜態資源(示例):
<DataTemplate x:Key="FirstNamesTemplate">
<Label Content="{Binding FirstName}"/>
</DataTemplate>
ComboBoxCard 的實作現在看起來像這樣:
<local:CardComboBox ItemsSource="{Binding PersonModels}" SelectedItem="{Binding SelectedPersonModel, Mode=TwoWay}" ItemContentTemplate="{StaticResource FirstNamesTemplate}" TitleText="With StaticResource" IsEnabled="False"/>
此模式允許在自定義 UserControls 中實作 ComboBoxes 或 ListViews。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/412040.html
標籤:
