标签: ivalueconverter

DataContext作为资源中转换器绑定的源

 <Canvas.DataContext>
  <ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
 </Canvas.DataContext>

 <!-- DataContext is not passed into these Instances.
      they also have no knowledge of their TemplatedParent. -->
 <Canvas.Resources>

  <!--  is there a way to use a binding that points to the datacontext within the resources ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{Binding Path=Model.SomeProperty}" />

  <!--  is there a way to point Directly to the TemplatedParent  ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{TemplateBinding SomeProperty}" />

 </Canvas.Resources>


 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />

 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, …
Run Code Online (Sandbox Code Playgroud)

wpf datacontext binding controltemplate ivalueconverter

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

使用值转换器绑定到Silverlight 4中的FontWeight

我想比较各种属性的两个版本,如果它不等于另一个,则加粗其中一个.由于SL4不支持MultiBinding,我将FontWeight绑定到".".以便将整个数据上下文传递给转换器.然后,我使用converter参数指定要在转换器中比较的字段.到目前为止,这么好......不匹配的值是粗体.

问题是粗体属性绑定到可以编辑的文本框.编辑该值时,我希望"重新激活"转换器,以便根据值设置字体粗细.这不会发生.如何实现这一目标?

注意:我已经为相关的类和属性实现了INotifyPropertyChanged.更改值后切换到下一个字段会导致PropertyChanged事件触发,但在我实际移动到另一个记录然后返回到已更改的记录之前,字体权重不会更新.

(我也尝试使用Mode = TwoWay来查看是否可以解决问题.但是,当你绑定到"."时,不能使用TwoWay绑定.)

silverlight xaml binding ivalueconverter

5
推荐指数
1
解决办法
1551
查看次数

MarkupExtension.ProvideValue - 是否实际使用了IServiceProvider?

我正在阅读我的一些旧代码并遇到了混合IValueConverter/ MarkupExtension类.这让我想知道如果IServiceProviderProvideValue方法实际上是有用的,以及它如何将有用吗?

我看到IServiceProvider只有一个方法:GetService必须转换为适当的服务类型.我还查看了MarkupExtension.ProvideValue MSDN页面,它列出了不同类型的服务.我想,我只是想知道这些服务是否有用,或者我应该保留我的方法呢?

现行方法:

public Object ProvideValue(IServiceProvider serviceProvider)
{
    return new MyConverter();
}
Run Code Online (Sandbox Code Playgroud)

基本上,我应该做以下事情:

public Object ProvideValue(IServiceProvider serviceProvider)
{
    var provider = serviceProvider as SomeType;

    if (provider == null) return new MyConverter();

    //Do something with the provider here?
}
Run Code Online (Sandbox Code Playgroud)

c# wpf markup-extensions ivalueconverter service-provider

5
推荐指数
2
解决办法
3619
查看次数

XAML找不到转换器类

我正在显示一个包含以下代码的弹出窗口:

<Popup PlacementTarget="{Binding ElementName=categoryTagEditorControl}"
       Placement="Bottom">
    <Popup.IsOpen>
        <MultiBinding Mode="OneWay" Converter="{StaticResource BooleanOrConverter}">
            <Binding Mode="OneWay" ElementName="categoryTagEditorControl" Path="IsMouseOver"/>
            <Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver" />
        </MultiBinding>
    </Popup.IsOpen>
    <StackPanel>
        <TextBox Text="Some Text.."/>
        <DatePicker/>
    </StackPanel>
</Popup>
Run Code Online (Sandbox Code Playgroud)

这是BooleanOrConverter的代码:

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (object booleanValue in values)
        {
            if (booleanValue is bool == false)
            {
                throw new ApplicationException("BooleanOrConverter only accepts boolean as datatype");
            }
            if ((bool)booleanValue == true)
            {
                return true;
            }
        }
        return false;
    } …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml ivalueconverter

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

数据绑定ItemsControl(DataTemplate)不更新/仅接收程序启动时的值

编辑2:当图表填充时,我无法再更改值.即使我更改列表中的值(ItemControls从中获取值),图表似乎也不会使用新值更新.

我调用计时器中的GetDataGrafiek()方法每隔x秒更新一次我的图表.

        Grafiek graf = new Grafiek();
        graf.GetDataGrafiek();
Run Code Online (Sandbox Code Playgroud)

这是因为线程计时器在一个单独的线程中运行(IsAsynchronous方法中ObjectDataProvider)还是我需要DataContext在我的计时器方法中访问ItemsControl?

编辑:我无法在程序运行时填充图表,所以我创建了ObservableCollection<GrafiekBar>静态(保存条形的填充和值的列表)并初始化如下:

public static ObservableCollection<GrafiekBar> listGrafiek = new ObservableCollection<GrafiekBar>()
        {
            new GrafiekBar() {Value = 0, Fill = (Brush)convertor.ConvertFromString(kleuren[0])},
            new GrafiekBar() {Value = 0, Fill = (Brush)convertor.ConvertFromString(kleuren[1])},
            new GrafiekBar() {Value = 0, Fill = (Brush)convertor.ConvertFromString(kleuren[2])}
        };
Run Code Online (Sandbox Code Playgroud)

从MSDN:ObjectDataProvider:"但是,如果您绑定到已创建的对象,则需要在代码中设置DataContext,如下例所示."


我有一个ItemsControl,显示为一个简单的条形图.当我分配值(在我的codeBehind中硬编码)时,图表会成功填充.

我所做的基本上是将最大值设置为100%并计算其余条形的长度.

问题: 我不希望图表栏值硬编码,但条形图必须更改runTime.为此,我使用的Threading.Timer是每当我的程序运行时每秒运行一次(此计时器中也会发生其他计算).

图表栏值根据每x秒内在此计时器内发生的计算得到更新.

我已经尝试了所有内容,但在程序运行时我无法显示值.当我对它们进行硬编码时,我只能看到条形图(参见GetDataGrafiek()线程末尾的区域).我究竟做错了什么/失踪了?

GetDataGrafiek()(计算来填充我的图表)被调用的ObjectDataProvider.

此方法将TimeSpan作为输入,然后执行计算,以便获得Double Value(基于上面解释的100%值),然后将其放置在bar的值(= dateTemplate的宽度)中.

<ObjectDataProvider x:Key="odpLbGrafiek" ObjectType="{x:Type myClasses:GrafiekBar}" MethodName="GetDataGrafiek"/> …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml ivalueconverter

5
推荐指数
1
解决办法
990
查看次数

转换值时WPF绑定引擎的特殊值是什么?

我已经知道可以从IValueConverter实现返回的Binding.DoNothing,表示不应该进行其他操作.

但是,我找不到一个很好地总结的参考或文档,其他特殊值是什么 - 比如返回后备值.这些是什么?

.net wpf ivalueconverter imultivalueconverter

5
推荐指数
1
解决办法
2428
查看次数

仅在DataGrid上滚动时才调用IValueConverter

看一下以下XAML代码段:

<DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Block.TextAlignment" Value="Center"/>
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        <MultiBinding Converter="{StaticResource VAPBrushConverter}">
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}"/>
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
                        </MultiBinding>
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.CellStyle>
Run Code Online (Sandbox Code Playgroud)

在滚动数据网格时才调用IValueConverter 。在DataGridCell中,有一个TextBlock,并且只有在TextBlock.Text属性为DependencyProperty.UnsetValue时,才调用IValueConverter。

有人可以告诉我何时调用IValueConverter,并且当前是否可以使用我的代码来解决此问题?澄清一下-问题是,当我在DataGrid上滚动时,背景仅由IValueConverter设置。

wpf xaml ivalueconverter multibinding

5
推荐指数
1
解决办法
327
查看次数

在IValueConverter类中定义属性

我需要在转换器类中定义DependencyProperty,因为我需要这些数据来进行转换,而这个数据在另一个对象中,而不是我绑定的对象.

我的转换器类如下:

public class LEGOMaterialConverter   : DependencyObject, IValueConverter
{
    public DependencyProperty MaterialsListProperty = DependencyProperty.Register("MaterialsList", typeof(Dictionary<int, LEGOMaterial>), typeof(LEGOMaterialConverter));

    public Dictionary<int, LEGOMaterial> MaterialsList
    {
        get
        {
            return (Dictionary<int, LEGOMaterial>)GetValue(MaterialsListProperty);
        }
        set
        {
            SetValue(MaterialsListProperty, value);
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        LEGOMaterial material = null;

        MaterialsList.TryGetValue((int)value, out material);

        return material;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在Window.REsources区域实现它:

<Window.Resources>
    <local:LEGOMaterialConverter x:Key="MaterialsConverter" MaterialsList="{Binding Path=Materials}" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误: …

wpf dependency-properties ivalueconverter

5
推荐指数
1
解决办法
4806
查看次数

确定任何类型的列表,序列,数组或IEnumerable是否为空

我正在使用XAML为我的视图编写一个Xamarin.Forms应用程序,我正在尝试编写一个IValueConverter应该返回的工作,false如果输入对于那些语义有意义的类型是"空"(strings/lists/sequences/arrays/IEnumerables) ).我已经开始使用以下内容,它为空字符串返回false,但我无法弄清楚如何将它扩展到列表,序列,数组和IEnumerables:

type FalseIfEmptyConverter() =
  interface IValueConverter with 
    member __.Convert(value:obj, _, _, _) = 
      match value with
      | :? string as s -> (s <> "" && not (isNull s)) |> box
      // TODO: extend to enumerables
      | x -> invalidOp <| "unsupported type " + x.GetType().FullName

    member __.ConvertBack(_, _, _, _) =
      raise <| System.NotImplementedException()
Run Code Online (Sandbox Code Playgroud)

我尝试过的东西不起作用:

  • :? list<_> 与(盒装)列表(至少不是整数)不匹配并产生警告 This construct causes code to be less generic than indicated by its type annotations. The type …

f# ivalueconverter

5
推荐指数
1
解决办法
180
查看次数

XAML资源字典中的算术运算

我想做的事

我最近一直在探索XAML资源词典.它们非常强大,但是为了减少(甚至进一步)为了适应任何修改而需要进行的更改,我想使用一些基本的算术运算来改变它的HeightRequest属性Entry.

我已经在充分利用OnPlatformOnIdiom不同方面,比如FontSize.

对于iOS平台,我想做HeightRequest一个条目20+(FontSize).将FontSize使用已经设置OnIdiom(它略有增加粒).

在一个完美的世界里,我正在努力做的核心事情可能看起来像
<Setter Property="HeightRequest" Value="{DynamicResource StandardFontSize}+10">

什么"有效"

我有一个工作的解决方案,如果我使用的组合OnIdiomOnPlatform.

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinDesigner.App"
             xmlns:local="clr-namespace:XamarinDesigner"
             >
    <Application.Resources>
        <ResourceDictionary>
            <OnIdiom x:Key="StandardFontSize" x:TypeArguments="x:Double"  Tablet="22" Phone="18"/>
            <Style x:Key="MyEntry" TargetType="Entry">
                <Setter Property="FontSize" Value="{DynamicResource StandardFontSize}"/>
                <Setter Property="HeightRequest">
                    <Setter.Value>
                        <OnIdiom x:TypeArguments="x:Double">
                            <OnIdiom.Phone>
                                <OnPlatform x:TypeArguments="x:Double" iOS="30"/>
                            </OnIdiom.Phone>
                            <OnIdiom.Tablet>
                                <OnPlatform x:TypeArguments="x:Double" iOS="40"/>
                            </OnIdiom.Tablet>
                        </OnIdiom>
                    </Setter.Value>
                </Setter>
                <Setter Property="VerticalOptions" Value="Center"/> …
Run Code Online (Sandbox Code Playgroud)

xaml resourcedictionary ivalueconverter xamarin.forms

5
推荐指数
1
解决办法
432
查看次数