与转换器绑定 [英] Binding with Converters

查看:63
本文介绍了与转换器绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<DataTemplate x:Key="sharps">
            <StackPanel  Orientation="Horizontal"  TextBlock.FontSize="30" >
                <StackPanel.RenderTransformOrigin>
                    <Point X="0.5" Y="0.5"/>
                </StackPanel.RenderTransformOrigin>
                <StackPanel.RenderTransform>
                    <TranslateTransform  Y="{Binding Midi, Converter={StaticResource MidiToOffsetSize}}"/>
                </StackPanel.RenderTransform>
                <TextBlock FontSize="18"   VerticalAlignment="Center" Foreground="Black"   Text="{Binding CumTicks,Converter={StaticResource CumTicksConverter}}"  />
                <TextBlock  FontFamily="notess"  Text="{Binding Pitch}"/>
                <TextBlock Foreground="Red" FontFamily="HarmonyMKT" Text="{Binding Notes}" />
            </StackPanel>
        </DataTemplate>





这是一个用于以某种文字处理器方式显示音符列表的数据模板。每个笔记都是模型的一个实例。一切都与视图模型中可观察的一组音符绑定。 StackPanel中的第一个TextBlock使用Converter(CumulativeTicksConverter)绑定到模型中的Midi字段,以便能够根据时间签名显示音乐中的条形线。时间签名是viewmodel中的一个字符串属性,我想在视图中绑定它,这样我就可以轻松更改时间签名。我的问题是这样的: - 如何从视图模型到转换器中获取此字符串。我所能做的就是将字符串硬编码为viewmodel中的静态属性,并引用转换器中的实例。如果我尝试多绑定,我可以将字符串作为绑定参数传递,但是如何将viewmodel中的非静态属性传递到数据模板中,其中所有绑定(我推测)都是模型中的属性,而不是viewmodel。



我在数据模板中使用的值转换器如下所示,问题字段带下划线,即TPB(代表每条音乐的midi标记)。

公共类CumTicksToBarConverter:IValueConverter

{

对象IValueConverter.Convert(对象值,类型targetType,对象参数,CultureInfo文化)
{



int ct =(int)value;

int商,余数;

商= Math.DivRem(ct, MainViewModel.TPB ,输出余数);

if(余数== 0)返回商+ 1;否则返回;

}



对象IValueConverter.ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化)

{

抛出新的NotImplementedException();



}

}





TPB是对viewmodel中静态字段的引用,如下所示。

public static int TPB {get;组; }。

我不知道如何在视图中绑定此值以更改其值。我想转换器只在xaml构造函数中创建一次(我是否正确?)但我希望能够在视图中选择TPB并在创建转换器之前将其传递给转换器。我该怎么做呢??



理想情况下,我希望在主窗口加载之前运行启动屏幕。用户可以输入歌曲的标题及其时间签名,但我需要帮助这样做。然后我想在主窗口xaml中构建转换器之前将此时间签名传递给viewmodel中的属性TPB。

任何帮助非常感谢。



This is a data template for displaying a list of musical notes in a sort of word processor fashion. Each note is an instance of the model. Everything is bound to an observable collection of notes in the view model. The first TextBlock in the StackPanel is bound to a Midi field in the model using a Converter (CumulativeTicksConverter) in order to be able to display the bar lines in the music depending on the time signature. The time signature is a string property in the viewmodel which I want to bind to in the view so I can change the time signature easily. My problem is this:- HOW CAN I GET THIS STRING FROM THE VIEW MODEL INTO THE CONVERTER. All I have been able to do is hard code the string as a static property in the viewmodel and reference the instance in the converter. If I try a multibinding, I could pass the string as a binding parameter but how do I pass a non static property in the viewmodel into the data template where all the bindings (I presume) are with properties in the model , not the viewmodel.

The value converter I use in the data template is shown below with the problem field underlined ie TPB ( which stands for midi ticks per bar of music).
public class CumTicksToBarConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{

int ct = (int)value;
int quotient, remainder;
quotient = Math.DivRem(ct, MainViewModel.TPB, out remainder);
if (remainder == 0) return quotient + 1; else return "";
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();

}
}


TPB is a reference to a static field in the viewmodel as shown here.
public static int TPB { get; set; }.
I don't know how to bind to this value in the view in order to change its value. I suppose the converter is only created once in the xaml constructer (am I correct?) but I would like to be able to choose TPB in the view and pass this to the converter BEFORE the converter is created. How do I do this??

Ideally I would like to run a startup screen before the main window loads. The user could type in a title for the song and its time signature but I need help doing this. I would then want to pass this time signature to the property TPB in the viewmodel before the converter is constructed in the main window xaml.
Any help much appreciated.

推荐答案

解决了问题。我应该已经意识到,虽然绑定类型转换器只能创建一次,但转换器中的任何静态字段都可以从视图模型中轻松访问和更改。所以我现在可以在视图中绑定组合中的时间签名列表,并使用它将我的TPB值传播到转换器中。它就像一个魅力。很抱歉浪费了任何时间。
Problem Solved. I should have realised that although a binding type converter may only be created once, any static fields in the converter can be accessed and changed easily from the view model. So I can now bind a list of time signatures from a combo in the view and use it to propagate my TPB value into the converter. It works like a charm. Sorry to have wasted any ones time.


这篇关于与转换器绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆