标签: valueconverter

WPF ValueConverter - 不可转换价值的标准回报

在过去一年左右的时间里,我看到许多不同的价值转换器出于许多不同的目的,来自许多不同的作者.在我的脑海中突出的一件事是它们返回的"默认"值的广泛变化.例如;

  public class MyConverter: IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      // OK, we test for some undesirable, unconvertable situation, typically null...
      if (value == null)
      {
        // And here are a variety of 'defaults' that I have seen, these begin the most typical.
        return null;
        return DependencyProperty.UnsetValue;
        return Binding.DoNothing;
      }
        //...... other code.. whatever...
}}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,是否有一种"标准"方式来表明输入值无法转换?

c# wpf ivalueconverter valueconverter

43
推荐指数
3
解决办法
1万
查看次数

将二进制字符串转换为二进制或十进制值

是否有任何函数将二进制字符串转换为二进制或十进制值?

如果我有一个二进制字符串000101,我应该怎么做才能将它转换成5

binary r typeconverter valueconverter bitconverter

20
推荐指数
4
解决办法
2万
查看次数

十六进制到int C#,非常大的数字

我有一个256字符长的字符串,其中包含一个十六进制值:

EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6CB3EF8C5BAA2A5E531BA9E28592F99E0FE4F95169A6C63F635D0197E325C5EC76219B907E4EBDCD401FB1986E4E3CA661FF73E7E2B8FD9988E753B7042B2BBCA76679

我想将它转换为带有这样的数字的字符串:

102721434037452409244947064576168505707480035762262934026941212181473785225928879178124028140134582697986427982801088884553965371786856967476796072433168989860379885762727214550528198941038582937788145880903882293999022181847665735412629158069472562567144696160522107094738216218810820997773451212693036210879

怎么能用如此大的数字来完成?

提前致谢.

c# biginteger valueconverter

16
推荐指数
1
解决办法
4613
查看次数

如何将任何对象转换为字符串?

这是我的代码:

for (String toEmail : toEmailList)          
{
    Log.i("GMail","toEmail: "+toEmail);
    emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));      
}
Run Code Online (Sandbox Code Playgroud)

请给我一些关于此的建议.

java string android valueconverter android-fragments

16
推荐指数
4
解决办法
12万
查看次数

无法在WPF中找到静态引用的枚举类型

我正在尝试将枚举绑定到WPF中的单选按钮(受​​此答案的启发),但我无法找到转换器参数的枚举类型:

枚举按以下方式定义

namespace Application.Models
{
    public class Enums
    {
        public enum MySelections { one, two ,three };

        public MySelections CurrentSelection;

        ...

    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试绑定这样的复选框(假设数据上下文是正确的并且实现了值转换器:)

<Window x:Class="Application.MainWindow"
        ....
        xnlns:models="clr-namespace:Application.Models" >

...
<RadioButton Content="One"
             IsChecked="{Binding Path=CurrentSelection, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static models:Enums.MySelections.one}}" />
...
Run Code Online (Sandbox Code Playgroud)

问题在于{x:Static models:Enums.MySelections.one}不断抛出models:Enums.MySelections无法找到类型的错误.

我怎样才能找到我的枚举类型?

wpf enums .net-4.0 valueconverter c#-4.0

14
推荐指数
2
解决办法
9432
查看次数

缩放两个数字范围

我记得在某些时候使用方程来做到这一点 - 你如何在Javascript中做到这一点?

插件两个数字范围:

rangeX = 1 (through) 10;
rangeY = 300.77 (through) 559.22;
Run Code Online (Sandbox Code Playgroud)

输入范围Y比例中的值:

inputY = 328.17;
Run Code Online (Sandbox Code Playgroud)

转换为rangeX比例的比例值:

outputX = 1.43;
Run Code Online (Sandbox Code Playgroud)

javascript valueconverter

14
推荐指数
4
解决办法
1万
查看次数

如何在WPF DataGrid中显示数组元素?

我正在尝试在WPF DataGrid中显示一系列行,其中每行包含一个布尔数组(其数量对于所有行都相同,它不是锯齿状的2D数组),我想将其显示为单独的列,例如.

Name            | Day 1 | Day 2 | Day 3 | Day 4 | Day 5 | Day 6 |
-----------------------------------------------------------------
Bring out Trash |   X   |       |   X   |       |       |   X   |
Pay Bills       |       |       |       |       |   X   |       |
Commit Suicide  |       |       |       |       |       |   X   |
Run Code Online (Sandbox Code Playgroud)

目前,我正在将此类用于我的DataGrid行:

private class GridRow {
  public string Name { get; set; }
  public char Day1 { get; set; }
  public char Day2 { get; …
Run Code Online (Sandbox Code Playgroud)

.net wpf datagrid valueconverter

11
推荐指数
1
解决办法
1万
查看次数

使用资源作为转换会导致绑定转换器

当我尝试将valueconverter从定义的枚举状态绑定到画笔时,我的XAML设计器中出现错误:

找不到'OKStatus'资源.

该应用程序运行良好的运行时,但我无法在设计器中看到我的GUI.我的资源在color.xaml文件中定义,该文件在运行时读取.所有代码都在同一名称空间内

我的XAML:

的xmlns:配置= "CLR的命名空间:App.MyNamespace"

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="c:\Skins\Colors.xaml" />
            <ResourceDictionary Source="c:\Skins\Common.xaml" />                
        </ResourceDictionary.MergedDictionaries>
        <config:StatusConverter x:Key="StateConverter" />
        <config:BoolConverter x:Key="BoolConverter" />
        <config:BooleanConverter x:Key="BooleanConverter" />
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

状态

我的转换器:

[ValueConversion(typeof(bool), typeof(Brush))]
public class BoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        bool state = (bool)value;

        FrameworkElement FrameElem = new FrameworkElement();

        if (state == true)
            return (FrameElem.FindResource("OKStatus") as Brush);
        else
            return (FrameElem.FindResource("ErrorStatus") as Brush);
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, …
Run Code Online (Sandbox Code Playgroud)

wpf valueconverter staticresource

11
推荐指数
3
解决办法
1万
查看次数

如何在Windows Phone 8中将ValueConverter用作StaticResource

下面是我的App.xaml

<Application
    x:Class="SpinrWindowsMobile.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    >

    <!--Application Resources-->
    <Application.Resources >
        <ResourceDictionary>

            <local:LocalizedStrings xmlns:local="clr-namespace:SpinrWindowsMobile" x:Key="LocalizedStrings"/>
            <converter:TextColorConverter xmlns:converter="clr-namespace:SpinrWindowsMobile.Common" x:Key="TextColorConverter"></converter:TextColorConverter>

        </ResourceDictionary>
    </Application.Resources>

    ....
</Application>
Run Code Online (Sandbox Code Playgroud)

我在NameSpace SpinrWindowsMo​​bile.Common中编写了TextColorConverter.cs, 同时启动应用程序它给我异常无法创建类型SpinrWindowsMo​​bile.Common.TextColorConverter的实例.我不知道我错过了哪里.下面是我的TextColorConverter.cs

class TextColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // some code
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           // some code
        }
    }
Run Code Online (Sandbox Code Playgroud)

我使用Microsoft Visual Studio 2012 for Windows Phone作为我的开发工具.我要分享的另一件事我没有在System.Windows.Data命名空间中获得ValueConverstionAttribute类.任何人都可以指导我在哪里错了.

c# xaml ivalueconverter valueconverter windows-phone-8

9
推荐指数
1
解决办法
5274
查看次数

WPF Richtextbox Bindable in .net 4.5

所以我试图在我的.net 4.5项目中使用David Veeneman的Bindable WPF RichTextBox.添加控件后,ValueConverter在我的代码中,我注意到只会public object Convert()触发但public object ConvertBack()不会触发.

在阅读了对该项目的评论后,我更改了控制源代码的以下部分.

private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var thisControl = (EcoRichTextBox)d;
    if (thisControl.m_InternalUpdatePending > 0)
    {
        thisControl.m_InternalUpdatePending--;
        return;
    }
    // Changed:
    try
    {
        thisControl.TextBox.Document = (e.NewValue == null) ? new FlowDocument() : (FlowDocument)e.NewValue;
    }
    catch { }
    thisControl.m_TextHasChanged = false;
} 
Run Code Online (Sandbox Code Playgroud)

而这个事件处理程序:

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
    // Set the TextChanged flag
    m_TextHasChanged = true;

    // Changed:
    Document = …
Run Code Online (Sandbox Code Playgroud)

wpf richtextbox mvvm valueconverter .net-4.5

9
推荐指数
1
解决办法
645
查看次数