私の 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 を作成しました。
ComboItems.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 にバインドされていません。その値は null のままです。
これを双方向で実現したい。つまり、RockDensity_Unit プロパティの値がプログラムで設定されている場合、ドロップダウンで値を選択する必要があります。もちろん、値はリストに存在する必要があります。
デフォルトでは、最初の項目が選択されているはずです。
更新 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>
エラー
エラー 1 タイプ参照は、'ucStep2' という名前のパブリック タイプを見つけることができません。Line 74 Position 194. これは、FindAncestor の後のコンボボックス ", " を指します。
疑問Step2InfoData の 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">