【问题标题】:If condition in XAML如果 XAML 中的条件
【发布时间】:2019-01-16 02:17:09
【问题描述】:

是否可以通过 XAML 为 wpf 实现 if 条件,如果可以,请提供 if 条件的框架以检查 xaml 中的 .net 框架版本。

我正在为 Visual Studio 2010 和 2008 共享 xaml,我想为 vs2010 执行特定的代码 sn-p 并为 vs2008 隐藏相同的代码

【问题讨论】:

  • 我正在为 Visual Studio 2010 和 2008 共享 xaml,我想为 vs2010 执行特定的代码 sn-p 并为 vs2008 隐藏相同的代码..
  • 你为什么只想在 XAML 中这样做?
  • 你想做什么?
  • 也许你应该看看触发器,它们提供了在 XAML 中进行条件处理的方法,看看转换器

标签: wpf xaml


【解决方案1】:

是的,他们被称为Triggers。有不同的类型,它们会对数据绑定的数据值的变化、触发的事件甚至用户界面控件的状态做出反应。您可以在 Code Project 上的 Triggers in WPF 帖子中找到描述它们的好文章。

编辑>>>

我刚刚将您的评论添加到您的问题中,我不得不说将您问题的如此重要部分放入评论中并不是一个好主意......因此,到目前为止,大多数答案都没有回答您的问题实际问题。出于这个原因,我对你的问题投了反对票。

在回答您的实际问题时,不,我不认为您可以使用可以识别 Visual Studio 版本之间差异的条件 XAML。如果您告诉我们您的最终目标是什么,那么我们也许仍然能够提供帮助。

【讨论】:

    【解决方案2】:

    看看在您的 XAML 中使用转换器。使用转换器,您可以通过执行代码在 XAML 中做出决策。例如,这将使我的按钮在某些条件下可见:

    XAML

    <Button
        x:Name="btnMyButton"
        Margin="2,0"
        Command="{Binding Model.MyButtonCommand}"
        Content="My Text"
        Visibility="{Binding Model.IsShowMode, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=False}"
    />
    

    C#类

    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
       {
        // Do the conversion 
       }
    
       public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
          {
          // UnDo conversion
      }
    }
    

    【讨论】:

    • 记得在 xaml 的 Window.Resources 元素中添加对转换器的引用。 &lt;Window.Resources&gt; &lt;local: BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/&gt; &lt;/Window.Resources&gt;
    【解决方案3】:

    也许你可以看看这个http://josheinstein.com/blog/2010/06/switchconverter-a-switch-statement-for-xaml/

    我没有尝试过,但它似乎适合你。

    编辑:

    这是 SwitchConverter 的代码,因为 Josh's 站点似乎已关闭 -

    /// <summary>
    /// A converter that accepts <see cref="SwitchConverterCase"/>s and converts them to the 
    /// Then property of the case.
    /// </summary>
    [ContentProperty("Cases")]
    public class SwitchConverter : IValueConverter
    {
        // Converter instances.
        List<SwitchConverterCase> _cases;
    
        #region Public Properties.
        /// <summary>
        /// Gets or sets an array of <see cref="SwitchConverterCase"/>s that this converter can use to produde values from.
        /// </summary>
        public List<SwitchConverterCase> Cases { get { return _cases; } set { _cases = value; } }
        #endregion
        #region Construction.
        /// <summary>
        /// Initializes a new instance of the <see cref="SwitchConverter"/> class.
        /// </summary>
        public SwitchConverter()
        {
            // Create the cases array.
            _cases = new List<SwitchConverterCase>();
        }
        #endregion
    
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // This will be the results of the operation.
            object results = null;
    
            // I'm only willing to convert SwitchConverterCases in this converter and no nulls!
            if (value == null) throw new ArgumentNullException("value");
    
            // I need to find out if the case that matches this value actually exists in this converters cases collection.
            if (_cases != null && _cases.Count > 0)
                for (int i = 0; i < _cases.Count; i++)
                {
                    // Get a reference to this case.
                    SwitchConverterCase targetCase = _cases[i];
    
                    // Check to see if the value is the cases When parameter.
                    if (value == targetCase || value.ToString().ToUpper() == targetCase.When.ToString().ToUpper())
                    {
                        // We've got what we want, the results can now be set to the Then property
                        // of the case we're on.
                        results = targetCase.Then;
    
                        // All done, get out of the loop.
                        break;
                    }
                }
    
            // return the results.
            return results;
        }
    
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Represents a case for a switch converter.
    /// </summary>
    [ContentProperty("Then")]
    public class SwitchConverterCase
    {
        // case instances.
        string _when;
        object _then;
    
        #region Public Properties.
        /// <summary>
        /// Gets or sets the condition of the case.
        /// </summary>
        public string When { get { return _when; } set { _when = value; } }
        /// <summary>
        /// Gets or sets the results of this case when run through a <see cref="SwitchConverter"/>
        /// </summary>
        public object Then { get { return _then; } set { _then = value; } }
        #endregion
        #region Construction.
        /// <summary>
        /// Switches the converter.
        /// </summary>
        public SwitchConverterCase()
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SwitchConverterCase"/> class.
        /// </summary>
        /// <param name="when">The condition of the case.</param>
        /// <param name="then">The results of this case when run through a <see cref="SwitchConverter"/>.</param>
        public SwitchConverterCase(string when, object then)
        {
            // Hook up the instances.
            this._then = then;
            this._when = when;
        }
        #endregion
    
        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format("When={0}; Then={1}", When.ToString(), Then.ToString());
        }
    }
    

    【讨论】:

    • 博客似乎已关闭。你能总结一下或提供一个替代博客吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2010-11-15
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多