我使用 Silverlight 4。有一个包含项目列表的 ComboBox。这些项目有“ToString()”:
public class SquadFlangViewModel
{
static SquadFlangViewModel()
{
int iFlangValuesCount = (typeof(TacticGameFlangFlangEnum)).GetFields().Length;
Names = new string[iFlangValuesCount];
Names[(int)TacticGameFlangFlangEnum.Usual] = "Равномерно";
Names[(int)TacticGameFlangFlangEnum.Left] = "Левый";
Names[(int)TacticGameFlangFlangEnum.Center] = "Центр";
Names[(int)TacticGameFlangFlangEnum.Right] = "Правый";
Names[(int)TacticGameFlangFlangEnum.Sides] = "Фланги";
Names[(int)TacticGameFlangFlangEnum.LeftAndCenter] = "Левый и Центр";
Names[(int)TacticGameFlangFlangEnum.RightAndCenter] = "Правый и Центр";
ShortNames = new string[iFlangValuesCount];
ShortNames[(int)TacticGameFlangFlangEnum.Usual] = "=";
ShortNames[(int)TacticGameFlangFlangEnum.Left] = "Л";
ShortNames[(int)TacticGameFlangFlangEnum.Center] = "Ц";
ShortNames[(int)TacticGameFlangFlangEnum.Right] = "П";
ShortNames[(int)TacticGameFlangFlangEnum.Sides] = "Ф";
ShortNames[(int)TacticGameFlangFlangEnum.LeftAndCenter] = "ЛЦ";
ShortNames[(int)TacticGameFlangFlangEnum.RightAndCenter] = "ПЦ";
}
public SquadFlangViewModel(TacticGameFlangFlangEnum eVal) { _eVal = eVal; }
public TacticGameFlangFlangEnum Value { get { return _eVal; } }
private static readonly string[] Names;
private static readonly string[] ShortNames;
public override string ToString()
{
return Names[(int)_eVal];
}
public string ShortName
{
get
{
return ShortNames[(int)_eVal];
}
}
}
这是我绑定元素的方式:
<ComboBox ItemsSource="{Binding Flangs, Mode=OneTime}"
SelectedItem="{Binding Flang, Mode=TwoWay}"/>
ComboBox 位于非常窄的网格单元格中的问题,当我选择项目时,我想显示的不是长描述,而是短描述。
听起来我应该指定 ComboBox 在元素显示为选中时使用“ShortName”属性。
当 ComboBox 折叠时,如何指定 ComboBox 以使用 ShortName 属性来显示所选元素?
非常欢迎任何想法和其他反馈。