我有BudgetControlType属性,其值为1 .. 7
if(BudgetControlType ==1)
dataComboBox1.Visibility=Visibility.Visiblile;
dataComboBox2 to dataComboBox7 =Visibility.Hidden;
if(BudgetControlType ==2)
dataComboBox1.Visibility=Visibility.Visiblile;
dataComboBox2.Visibility=Visibility.Visiblile;
dataComboBox3 to dataComboBox7 =Visibility.Hidden;
依旧......
如何在xaml中执行此操作?
答案 0 :(得分:4)
您可以使用1,2,4,8,...并将其转换为可见性
例如,如果您的int数是6(2 + 4),则使用参数2进行控制,使用参数4进行控制是可见的!
public class IntToVisibilityConverter:IValueConverter
{
private int val;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int intParam = (int)parameter;
val = (int)value;
return ((intParam & val) != 0) ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
在xaml:
<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=1}"/>
<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=2}"/>
<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=4}"/>
答案 1 :(得分:1)
我说的最好的方法是使用ViewModel上的属性,然后绑定它们。
例子(你必须按摩一下,但从这里开始相当简单):
public Visibility dtcb1 { get; set; }
// all the rest till 7
// Somewhere in your logit / constructor :
dtcb1 = BudgetControlType == 1 ? Visible : Hidden;
// and so on
在您的xaml上,您将自己的可见性与dtcb1
绑定
您可以将属性设置为boolean,并使用布尔到可见性转换器(例如,根据this answer,或者只是谷歌自己)
答案 2 :(得分:1)
这是我过去使用WPFConverters时使用的另一种方法。
<TabItem.Visibility>
<Binding Path="SomeObservableCollection.Count">
<Binding.Converter>
<converters:ConverterGroup>
<converters:ExpressionConverter Expression="{}{0} > 0" />
<BooleanToVisibilityConverter />
</converters:ConverterGroup>
</Binding.Converter>
</Binding>
</TabItem.Visibility>
ConvertGroup允许顺序运行多个转换器。
ExpressionConverter允许您定义任意表达式。在我的情况下,如果集合计数大于零,我希望TabItem可见。在xaml中定义意味着转义字符和一些有点笨拙的语法,但它运行得很好!
BooleanToVisibilityConverter将表达式的布尔结果转换为我们所需的可见性。
对于Elham,只要实现了INotifyPropertyChanged,就可以绑定BudgetControlType。一个equals表达式是这样完成的(如果绑定值等于7,我将返回true):
<converters:ExpressionConverter Expression="{}{0} == 7" />
答案 3 :(得分:0)
用于绑定可见性它更好地使用转换器
boolToVisibilityConverter类:
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((Visibility)value)
{
case Visibility.Collapsed:
case Visibility.Hidden:
return false;
case Visibility.Visible:
return true;
}
return null;
}
}
ViewModel中的:
public bool IsVisibile { get; set; }
控制器中的:
ViewModelCore.IsVisibile = false; //or true
以Xaml形式:
<UserControl.Resources> // or Window
<converter:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</UserControl.Resources>
<Button Visibility="{Binding IsVisibile ,Converter={StaticResource boolToVisibilityConverter}}" /> //or your controls