我有一個帶有兩個 ComboBox 的自定義控制元件,其中包含硬編碼的 ComboBoxItems,我希望能夠通過單個 DependencyProperty 系結到這些值,但似乎無法使系結作業。也許我的方法很遠,但我該怎么做才能讓它發揮作用?
通用.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp.Components">
<Style TargetType="{x:Type local:ContractLimitSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ContractLimitSelector}">
<ControlTemplate.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</ControlTemplate.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<ComboBox x:Name="PART_ComboAA" Grid.Column="0" SelectedValue="{TemplateBinding SelectedAA}" />
<ComboBox x:Name="PART_ComboFA" Grid.Column="1" SelectedValue="{TemplateBinding SelectedFA}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
自定義控制元件:
public class SelectorItems
{
public string AA { get; set; }
public string FA { get; set; }
}
public class ContractLimitSelector : Control
{
private static List<string> ComboBoxSource = new List<string>()
{
"-",
"BE",
"EE",
};
static ContractLimitSelector()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ContractLimitSelector), new FrameworkPropertyMetadata(typeof(ContractLimitSelector)));
}
public override void OnApplyTemplate()
{
var comboAA = Template.FindName("PART_ComboAA", this) as ComboBox;
var comboFA = Template.FindName("PART_ComboFA", this) as ComboBox;
comboAA.ItemsSource = ComboBoxSource;
comboFA.ItemsSource = ComboBoxSource;
base.OnApplyTemplate();
}
private SelectorItems GetAllValues()
{
var AA = (string)GetValue(SelectedAAProperty);
var FA = (string)GetValue(SelectedFAProperty);
return new SelectorItems() { AA = AA, FA = FA };
}
public static readonly DependencyProperty VisibleHeaderProperty =
DependencyProperty.Register("VisibleHeader", typeof(bool), typeof(ContractLimitSelector),
new PropertyMetadata(false));
public bool VisibleHeader
{
get { return (bool)GetValue(VisibleHeaderProperty); }
set { SetValue(VisibleHeaderProperty, value); }
}
public static readonly DependencyProperty SelectedAAProperty =
DependencyProperty.Register("SelectedAA", typeof(string), typeof(ContractLimitSelector),
new PropertyMetadata(string.Empty));
public string SelectedAA
{
get { return (string)GetValue(SelectedAAProperty); }
set
{
var all = GetAllValues();
SetValue(SelectedValuesProperty, new SelectorItems() { AA = value, FA = all.FA, });
SetValue(SelectedAAProperty, value);
}
}
public static readonly DependencyProperty SelectedFAProperty =
DependencyProperty.Register("SelectedFA", typeof(string), typeof(ContractLimitSelector),
new PropertyMetadata(string.Empty));
public string SelectedFA
{
get { return (string)GetValue(SelectedFAProperty); }
set
{
var all = GetAllValues();
SetValue(SelectedValuesProperty, new SelectorItems() { AA = all.AA, FA = value, });
SetValue(SelectedFAProperty, value);
}
}
public SelectorItems SelectedValues
{
get
{
return new SelectorItems()
{
AA = (string)GetValue(SelectedAAProperty),
FA = (string)GetValue(SelectedFAProperty),
};
}
set
{
SetValue(SelectedAAProperty, value.AA);
SetValue(SelectedFAProperty, value.FA);
}
}
public static readonly DependencyProperty SelectedValuesProperty =
DependencyProperty.Register("SelectedValues", typeof(SelectorItems), typeof(ContractLimitSelector),
new PropertyMetadata(new SelectorItems()));
}
我這樣使用它:XAML:
<custom:ContractLimitSelector SelectedValues="{Binding PrepareLockHouseValue, Mode=TwoWay}" />
視圖模型:
private SelectorItems _contractValues;
public SelectorItems ContractValues
{
get
{
return _contractValues;
}
set
{
_contractValues = value;
OnPropertyChanged(nameof(ContractValues));
}
}
我認為只要 ComboBoxes 的任何更改都會觸發“共享”SelectedValues DependecyProperty,就使用 SetValue,但沒有任何東西被觸發......
我錯過了什么?
uj5u.com熱心網友回復:
在SelectedAA與SelectedFA當你呼叫屬性setter不叫
SetValue(SelectedAAProperty, value.AA);
SetValue(SelectedFAProperty, value.FA);
你必須寫
SelectedAA = value.AA;
SelectedFA = value.FA;
最好移動所有代碼,除了
SetValue(SelectedAAProperty, value);
和
SetValue(SelectedFAProperty, value);
退出 setter 并添加PropertyChangedCallbacks以執行其他代碼。
然后,這些屬性也可以與資料系結和其他可能的值源一起正常作業。
通常,依賴項屬性的 CLR 包裝器的 get 和 set 方法不得包含除GetValue和SetValue呼叫之外的任何其他內容,因為框架可能會繞過 get/set。
有關詳細資訊,請參閱XAML 加載和依賴項屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/363187.html
