在我的 UserControl ucStep2 中,我有 Step2InfoData 对象的 DataContext,该对象具有多个属性以及:
private string rockDensUnit;
public string RockDensity_Unit
{
get { return rockDensUnit; }
set
{
if (rockDensUnit != value)
{
rockDensUnit = value;
Changed("RockDensity_Unit");
}
}
}
在我的应用程序中,我必须绑定几个具有不同通常测量类型的组合,例如{kg/m3,gm/m3},{meter,cm}等等这样的测量组。我的意思是,多个组合具有相同项目的列表。因此,我更喜欢创建此类列表的类,以便我可以在多个组合中使用。我创建了 ComboItems.cs,其中包含填充下拉列表所需的所有项目列表。
组合项.cs
//**OBJECTS I USE FOR LIST OF IEMS**
// Class for kg, gm
public class KgGmItems
{
public ObservableCollection<string> KgGmList { get; set; }
public KgGmItems()
{
KgGmList = new ObservableCollection<string>();
KgGmList.Add("kg/m3");
KgGmList.Add("gram/cm3");
}
public string ValueSelected { get; set; } // Don't know if this is useful in my case
}
// Class for meter, cm
public class MtCmItems : INotifyPropertyChanged
{
public MtCmItems()
{
Dict = new Dictionary<string, string>
{
{"meter", "meter"},
{"centimeter", "centimeter"}
};
}
//...
}
XML 即 ucStep2 视图
<!-- As the objects KgGmItems doesn't contain in ucStep2.xaml.cs or Step2InfoData (that is bound to this UC) so add reference of those classes -->
<UserControl.Resources>
<ObjectDataProvider x:Key="KgGmObj" ObjectType="{x:Type top:KgGmItems}" />
<ObjectDataProvider x:Key="MtCmObj" ObjectType="{x:Type top:MtCmItems}" />
</UserControl.Resources>
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedValue="{Binding Path=RockDensity_Unit, Mode=TwoWay}" SelectedIndex="0"
Background="#FFB7B39D" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="401,61,0,0" Name="comboBox6" VerticalAlignment="Top" Width="84" Visibility="Hidden">
</ComboBox>
我想显示 KgGmItems 类中的 ObservableCllection KgGmList 项目,并将所选值绑定到绑定到此 UserControl 的类 Step2InfoData 的 RockDensity_Unit。
在上面的组合中,我可以显示下拉列表中的所有项目,并且默认选择第一项。但该值没有绑定到RockDensity_Unit;它的值仍然为空。
我希望这种情况以两种方式发生,即当以编程方式设置 RockDensity_Unit proeprtiy 的值时,应在下拉列表中选择该值。当然该值应该存在于列表中。
默认情况下,应选择第一项。
UPDATE在 ucStep2.xaml.cs 中添加了 DependencyProperty
public static readonly DependencyProperty RockDensityUnitProperty =
DependencyProperty.Register("RockDensity_Unit", typeof(string), typeof(UserControl),
new FrameworkPropertyMetadata("kg/m3", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string RockDensity_Unit
{
get { return this.GetValue(RockDensityUnitProperty) as string; }
set { SetValue(RockDensityUnitProperty, value); }
}
XML
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedItem="{Binding Path=RockDensity_Unit, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ucStep2}}, Mode=TwoWay}"
Background="#FFB7B39D" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="401,61,0,0" Name="comboBox6" VerticalAlignment="Top" Width="84" Visibility="Hidden">
</ComboBox>
ERROR
错误 1 类型引用找不到名为“ucStep2”的公共类型。第 74 行位置 194。这指的是组合框“,”
找到祖先之后
DOUBTStep2InfoData 中的 RockDensity_Unit CLR 属性保持不变。
为什么代码找不到 ucStep2 ?仅供参考,我认为这可能相关:
<UserControl x:Class="WellBore.ucStep2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WellBore.Models"
xmlns:top="clr-namespace:WellBore"
mc:Ignorable="d"
d:DesignHeight="870" d:DesignWidth="700" MaxHeight="970" MinHeight="700" MaxWidth="600">