Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf tImplementedException(); } } /// ///表示开关转换器的情况。 /// [ContentProperty(“Then”)] 公共类SwitchConverterCase { //案例实例。 字符串_时; 那么对象!; #区域公共财产。 /// ///获取或设置案例的条件。 /// 当{get{return}u When;}set{u When=value;}时的公共字符串 /// ///获取或设置在通过 /// 公共对象然后{get{return}u Then;}set{u Then=value;} #端区 #区域建设。 /// ///切换转换器。 /// 公共SwitchConverterCase() { } /// ///初始化类的新实例。 /// ///案件的情况。 ///此案例在运行时的结果。 公共SwitchConverterCase(字符串when,对象then) { //连接实例。 这._then=then; 这个。_when=when; } #端区 /// ///返回表示此实例的。 /// /// ///表示此实例的。 /// 公共重写字符串ToString() { 返回string.Format(“When={0};Then={1}”、When.ToString()、Then.ToString()); } }_Wpf_Xaml_Binding_Switch Statement_Datatrigger - Fatal编程技术网

Wpf tImplementedException(); } } /// ///表示开关转换器的情况。 /// [ContentProperty(“Then”)] 公共类SwitchConverterCase { //案例实例。 字符串_时; 那么对象!; #区域公共财产。 /// ///获取或设置案例的条件。 /// 当{get{return}u When;}set{u When=value;}时的公共字符串 /// ///获取或设置在通过 /// 公共对象然后{get{return}u Then;}set{u Then=value;} #端区 #区域建设。 /// ///切换转换器。 /// 公共SwitchConverterCase() { } /// ///初始化类的新实例。 /// ///案件的情况。 ///此案例在运行时的结果。 公共SwitchConverterCase(字符串when,对象then) { //连接实例。 这._then=then; 这个。_when=when; } #端区 /// ///返回表示此实例的。 /// /// ///表示此实例的。 /// 公共重写字符串ToString() { 返回string.Format(“When={0};Then={1}”、When.ToString()、Then.ToString()); } }

Wpf tImplementedException(); } } /// ///表示开关转换器的情况。 /// [ContentProperty(“Then”)] 公共类SwitchConverterCase { //案例实例。 字符串_时; 那么对象!; #区域公共财产。 /// ///获取或设置案例的条件。 /// 当{get{return}u When;}set{u When=value;}时的公共字符串 /// ///获取或设置在通过 /// 公共对象然后{get{return}u Then;}set{u Then=value;} #端区 #区域建设。 /// ///切换转换器。 /// 公共SwitchConverterCase() { } /// ///初始化类的新实例。 /// ///案件的情况。 ///此案例在运行时的结果。 公共SwitchConverterCase(字符串when,对象then) { //连接实例。 这._then=then; 这个。_when=when; } #端区 /// ///返回表示此实例的。 /// /// ///表示此实例的。 /// 公共重写字符串ToString() { 返回string.Format(“When={0};Then={1}”、When.ToString()、Then.ToString()); } },wpf,xaml,binding,switch-statement,datatrigger,Wpf,Xaml,Binding,Switch Statement,Datatrigger,我根据公认的答案制作了一个简化、更新的转换器。 它还允许设置字符串比较和默认大小写: [ContentProperty("Cases")] public class SwitchConverter : IValueConverter { public SwitchConverter() { Cases = new List<SwitchConverterCase>(); } public List<SwitchConverter

我根据公认的答案制作了一个简化、更新的转换器。 它还允许设置字符串比较和默认大小写:

[ContentProperty("Cases")]
public class SwitchConverter : IValueConverter
{
    public SwitchConverter()
    {
        Cases = new List<SwitchConverterCase>();
    }

    public List<SwitchConverterCase> Cases { get; set; }

    public StringComparison StringComparisonType { get; set; } = StringComparison.InvariantCulture;

    public object Default { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || Cases == null)
        {
            return DependencyProperty.UnsetValue;
        }

        SwitchConverterCase result = Cases.FirstOrDefault(c => string.Equals(value.ToString(), c.When, StringComparisonType));
        return result != null ? result.Then : Default;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
用法示例:

<con:SwitchConverter x:Key="StyleConverter"
                     Default="{x:Static FontWeights.Normal}">
    <con:SwitchConverterCase When="pageHeader"
                             Then="{x:Static FontWeights.Bold}" />
    <con:SwitchConverterCase When="header"
                             Then="{x:Static FontWeights.SemiBold}" />
    <con:SwitchConverterCase When="smallText"
                             Then="{x:Static FontWeights.Light}" />
    <con:SwitchConverterCase When="tinyText"
                             Then="{x:Static FontWeights.Thin}" />
</con:SwitchConverter>

<TextBlock FontWeight="{Binding Style, Converter={StaticResource StyleConverter}}" />

或内联:

<TextBlock>
    <TextBlock.FontWeight>
        <Binding Path="Style">
            <Binding.Converter>
                <con:SwitchConverter Default="{x:Static FontWeights.Normal}">
                    <con:SwitchConverterCase When="pageHeader"
                                             Then="{x:Static FontWeights.Bold}" />
                    <!-- etc -->
                </con:SwitchConverter>
            </Binding.Converter>
        </Binding>
    </TextBlock.FontWeight>
</TextBlock>


事实上,严格以你为例,
{Binding MyValue,StringFormat='Value is{0}}}
我知道,但我想强调的是
开关的部分。@Shimmy,刚刚把你推过了10K:)@DanM是的,是你干的!!!谢谢♥♥♥谢谢我在myc问题中添加了一些内容。Dan的答案几乎是你们所能做的。我认为这是正确的答案,但我只能给他投票:(请看我更新的答案,也许你可以添加一些东西。你可以在DataTrigger中使用多个setter,setter上有一个名为
TargetName
的属性,这可能会帮到你(尽管我不确定你是否可以在这种情况下使用它).Link已失效,但您可以访问文章@akjoshi提供一个xaml示例是否很好?
/// <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());
    }
}
[ContentProperty("Cases")]
public class SwitchConverter : IValueConverter
{
    public SwitchConverter()
    {
        Cases = new List<SwitchConverterCase>();
    }

    public List<SwitchConverterCase> Cases { get; set; }

    public StringComparison StringComparisonType { get; set; } = StringComparison.InvariantCulture;

    public object Default { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || Cases == null)
        {
            return DependencyProperty.UnsetValue;
        }

        SwitchConverterCase result = Cases.FirstOrDefault(c => string.Equals(value.ToString(), c.When, StringComparisonType));
        return result != null ? result.Then : Default;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
[ContentProperty("Then")]
public class SwitchConverterCase
{
    public SwitchConverterCase()
    {
    }

    public SwitchConverterCase(string when, object then)
    {
        When = when;
        Then = then;
    }

    public string When { get; set; }

    public object Then { get; set; }

    public override string ToString() => $"When={When}; Then={Then}";
}
<con:SwitchConverter x:Key="StyleConverter"
                     Default="{x:Static FontWeights.Normal}">
    <con:SwitchConverterCase When="pageHeader"
                             Then="{x:Static FontWeights.Bold}" />
    <con:SwitchConverterCase When="header"
                             Then="{x:Static FontWeights.SemiBold}" />
    <con:SwitchConverterCase When="smallText"
                             Then="{x:Static FontWeights.Light}" />
    <con:SwitchConverterCase When="tinyText"
                             Then="{x:Static FontWeights.Thin}" />
</con:SwitchConverter>

<TextBlock FontWeight="{Binding Style, Converter={StaticResource StyleConverter}}" />
<TextBlock>
    <TextBlock.FontWeight>
        <Binding Path="Style">
            <Binding.Converter>
                <con:SwitchConverter Default="{x:Static FontWeights.Normal}">
                    <con:SwitchConverterCase When="pageHeader"
                                             Then="{x:Static FontWeights.Bold}" />
                    <!-- etc -->
                </con:SwitchConverter>
            </Binding.Converter>
        </Binding>
    </TextBlock.FontWeight>
</TextBlock>