标签: ivalueconverter

将字段的值传递给Silverlight ConverterParameter

我正在编写我的第一个Silverlight应用程序.我有一个数据网格,其列有两个标签,对于标签,我使用IValueConverter来有条件地格式化数据.

标签的"内容"设置如下:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}}"
Run Code Online (Sandbox Code Playgroud)

Content="{Binding AwayScore, Converter={StaticResource fmtshs}}"
Run Code Online (Sandbox Code Playgroud)

我的IValueConverter 的Convert方法是这样的:

Public Function Convert(
  ByVal value As Object, 
  ByVal targetType As System.Type, 
  ByVal parameter As Object, 
  ByVal culture As System.Globalization.CultureInfo) As Object 
Implements System.Windows.Data.IValueConverter.Convert

    Dim score As Long = value, other As Long = parameter

    Return If(score < 0, "", 
        If(score - other > 5, (other + 5).ToString, score.ToString)
    )

End Function
Run Code Online (Sandbox Code Playgroud)

所以我想做的是在HomeScore的转换器中,我想将AwayScore传递给ConverterParameter,而对于AwayScore我想将HomeScore传递给转换器.在任一分数的转换器中,我需要能够知道其他分数的值以用于格式化目的.

但我无法弄清楚将ConverterParameter绑定到另一个字段的语法.
我尝试过以下方法:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter=AwayScore}"  
Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter={AwayScore}}"  
Content="{Binding …
Run Code Online (Sandbox Code Playgroud)

datagrid ivalueconverter silverlight-3.0

12
推荐指数
3
解决办法
2万
查看次数

什么时候调用ConvertBack方法?

我知道,当即将显示数据时,Convert()会调用方法来转换数据,而是显示转换后的数据.

我想知道何时ConvertBack()调用方法?它的目的是什么?

我有一个DataGrid,其ItemSource被绑定到一些收集,我已经实现IValueConverter了集合中的项目的一个属性.当我IsReadOnly="True"(在DataGrid中)时,ConvertBack如果我双击单元格(为其IValueConverter实现)然后更改焦点,则调用方法.但是当我制造时IsReadOnly="False",ConvertBack无论我DataGrid对它及其细胞做什么,都不会调用方法.

请解释一下DataGrid细胞的这种行为和ConvertBack方法IValueConverter.

c# silverlight wpf binding ivalueconverter

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

如何在XAML中将静态值传递给IValueConverter

我想在我的WP7应用程序中使用从Web服务获取的静态文本.每个文本都有一个名称(indetifier)和一个Content属性.

例如,文本可能如下所示:

Name = "M43";
Content = "This is the text to be shown";
Run Code Online (Sandbox Code Playgroud)

然后我想将文本的名称(即标识符)传递给a IValueConverter,然后查找Name并返回文本.

我认为转换器看起来像这样:

public class StaticTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(value)).Content;
        }

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在XAML中:

<phone:PhoneApplicationPage.Resources>
    <Helpers:StaticTextConverter x:Name="StaticTextConverter" />
</phone:PhoneApplicationPage.Resources>

...

<TextBlock Text="{Binding 'M43', Converter={StaticResource StaticTextConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用,我不确定我是否正确地将值传递给转换器.

有人有什么建议吗?

c# xaml ivalueconverter windows-phone-7

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

将ObservableCollection <>绑定到TextBox

我有以数据形式从Web服务返回的数据,ObservableCollection<string>我希望将集合绑定到只读,TextBox以便用户可以选择数据并将其复制到剪贴板.

要将集合绑定到我创建的TextBox的Text属性,该TextBox IValueConverter将集合转换为文本字符串.这似乎有效,除了它只工作一次,就好像绑定不能识别对Observable集合的后续更改.这是一个简单的应用程序,重现问题,只是为了确认绑定工作正常我还绑定到`ListBox'

这是因为Text绑定简单不处理集合的更改事件吗?

一个选项当然是我处理集合更改并将它们传播到TextBox绑定的Text属性,这很好,但我想理解为什么在我看来是一个明显的解决方案是不行的正如所料.

XAML

<Window x:Class="WpfTextBoxBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTextBoxBinding"
        Title="MainWindow" Height="331" Width="402">
  <StackPanel>
    <StackPanel.Resources>
      <local:EnumarableToTextConverter x:Key="EnumarableToTextConverter" />
    </StackPanel.Resources>
    <TextBox Text="{Binding TextLines, Mode=OneWay, Converter={StaticResource EnumarableToTextConverter}}" Height="100" />
    <ListBox ItemsSource="{Binding TextLines}" Height="100" />
    <Button Click="Button_Click" Content="Add Line" />
  </StackPanel >
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Globalization;

namespace WpfTextBoxBinding
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : …
Run Code Online (Sandbox Code Playgroud)

data-binding silverlight wpf ivalueconverter

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

将Double绑定到TextBox

我经常使用TextBox绑定到Integers而没有太多问题.

但是,如果我尝试将TextBox绑定到Double,则它不起作用.

当我输入5,85(我的文化为decimalSeperator)时,我将585.0传递给double值.

它是如何转换的,我可以使用什么解决方案来解决这个问题?ValueConverter会是最好的解决方案吗?

data-binding wpf xaml ivalueconverter

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

具有绑定依赖属性的IValueConverter

我需要根据要绑定的对象中标识的单元系统确定运行时StringFormat的某些TextBlocks绑定.

我有一个具有依赖属性的转换器,我想绑定.Bound值用于确定转换过程.

public class UnitConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty IsMetricProperty =
        DependencyProperty.Register("IsMetric", typeof(bool), typeof(UnitConverter), new PropertyMetadata(true, ValueChanged));

    private static void ValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((UnitConverter)source).IsMetric = (bool)e.NewValue;
    }

    public bool IsMetric
    {
        get { return (bool)this.GetValue(IsMetricProperty); }
        set { this.SetValue(IsMetricProperty, value); }
    }

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (IsMetric)
            return string.Format("{0:0.0}", value);
        else
            return string.Format("{0:0.000}", value);
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf ivalueconverter

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

如何在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数据绑定和值转换器交互

我试图了解下面简化的repro代码背后的实际情况.

我有一个单独Window的a ListBox和a TextBlock绑定在一起(即master - > detail).然后我有一个带有几个属性的ViewModel - 一个字符串和一个日期.对于这个日期,我实现了一个值转换器(LongDateConverter).

Debug.WriteLine()在代码中有几个调用导致以下输出:

  • 启动应用
    • In converter: ConverterProblem.MainWindowViewModel
    • In converter: null
  • 单击列表框中的两个项目之一
    • In converter: ConverterProblem.DataModel

第二次和第三次调用IValueConverter我认为理解的方法.第二个是null因为ListBox还没有选定的项目.第三个是我选择的项目.

我不明白的是:

  1. 为什么第一个调用传递了类型的值MainWindowViewModel
  2. 为什么这个电话甚至首先发生?

这是我的代码:

MainWindow.xaml:

<Window x:Class="ConverterProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:ConverterProblem"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <app:LongDateConverter x:Key="longDateConverter"/>
    </Window.Resources>
    <StackPanel Orientation="Horizontal">
        <ListBox SelectedItem="{Binding Data}" ItemsSource="{Binding DataList}"
                 DisplayMemberPath="Name"/>
        <TextBlock Text="{Binding Converter={StaticResource longDateConverter}}" 
                   DataContext="{Binding Data}" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml ivalueconverter

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

Null To Boolean IValueConverter无法正常工作

如何使用IValueConverter将空值转换为布尔值?

我正在使用wpf来尝试显示一堆布尔值(在复选框中).创建新记录时,这些值为空,并在复选框中显示为"不确定".我希望空值出现并保存为"false"值.

我尝试创建一个NullToBoolean转换器,它从数据库中获取空值并将它们显示为false,然后在用户点击保存时将它们保存为false.(基本上,我试图避免用户必须在复选框中单击两次(一次使其成为真,然后再次使其成为假).这似乎适用于导入 - 即空值显示为false - 但除非我进行双击跳舞,当我保存时,数据库中的值不会改变.

我的转换器:

[ValueConversion(typeof(bool), typeof(bool))]
public class NullBooleanConverter : IValueConverter
{

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value != null)
    {
      return value;
    }
    return false;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value != null)
    {
      return value;
    }
    return null;
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图让Converter工作的复选框之一:

    <CheckBox Grid.Column="1" Grid.Row="0" Padding="5" Margin="5" VerticalAlignment="Center" Name="chkVarianceDescriptionProvided" IsThreeState="False">
      <CheckBox.IsChecked>
        <Binding Path="VarianceDescriptionProvided" Mode="TwoWay">
          <Binding.Converter>
            <utils:NullBooleanConverter />
          </Binding.Converter> …
Run Code Online (Sandbox Code Playgroud)

c# wpf null ivalueconverter

8
推荐指数
2
解决办法
7964
查看次数

如何在 IValueConverter 中使用 targetType 参数?

我有一个IValueConverter我想用它来做简单的数学运算,它具有以下Convert功能:

public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        if (parameter == null)
        {
            return value;
        }

        switch (((string)parameter).ToCharArray()[0])
        {
            case '%':
                return (double)value % double.Parse(
                    ((string)parameter).TrimStart(new char[] {'%'}));
            case '*':
                return (double)value * double.Parse(
                    ((string)parameter).TrimStart(new char[] {'*'}));
            case '/':
                return (double)value / double.Parse(
                    ((string)parameter).TrimStart(new char[] {'/'}));
            case '+':
                return (double)value + double.Parse(
                    ((string)parameter).TrimStart(new char[] {'+'}));
            case '-':
                if (((string)parameter).Length > 1)
                {
                    return (double)value - double.Parse(
                        ((string)parameter).TrimStart(new char[] {'-'}));
                }
                else …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml type-conversion ivalueconverter

7
推荐指数
1
解决办法
2667
查看次数