迅速地说,是否可以将两个滑块绑定到一个TextBox中?
基本上:
我想构建类似以下内容:
我知道如何将一个滑块绑定到一个文本框中,但是然后我不知道如何在同一文本框中以时间格式显示来自不同滑块的值\。
xaml:
<Calendar Margin="448,220,369,39" HorizontalContentAlignment="Center" Visibility="Visible" Name="calendarMain" SelectedDatesChanged="calendarMain_SelectedDatesChanged"/>
<TextBox HorizontalAlignment="Left" Text="{Binding Path=Value, ElementName=sliderMinutes, UpdateSourceTrigger=PropertyChanged}" Visibility="Visible" Name="txtboxCal" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="321,223,0,0"/>
<Slider HorizontalAlignment="Left" IsSnapToTickEnabled="True" Name="sliderHours" AutoToolTipPlacement="TopLeft" Minimum="0" Maximum="24" Margin="321,254,0,0" VerticalAlignment="Top" Width="120" Height="28"/>
<Slider HorizontalAlignment="Left" IsSnapToTickEnabled="True" Name="sliderMinutes" AutoToolTipPlacement="TopLeft" Minimum="0" Maximum="60" Margin="321,287,0,0" VerticalAlignment="Top" Width="120"/>
编辑
我设法像这样使用Multibinding做到了
<TextBlock Margin="836,423,107,25" Name="txtBlockTime">
<TextBlock.Text>
<MultiBinding StringFormat=" {0}:{1}">
<Binding ElementName="sliderHours" Path="Value"/>
<Binding ElementName="sliderMinutes" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
谢谢你的提示:)
答案 0 :(得分:0)
我建议您在这种情况下使用转换器。
<Window.Resources>
<local:TimeToStringMulti x:Key="TimeToStringMulti" />
</Window.Resources>
<TextBlock Margin="836,423,107,25" Name="txtBlockTime">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource TimeToStringMulti}" Mode="TwoWay">
<Binding ElementName="sliderHours" Path="Value"/>
<Binding ElementName="sliderMinutes" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
和Converter.cs
public class TimeToStringMulti: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.Format("{0}:{1}", values[0], values[1] );
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
string[] param = (value as string).Split(':');
return new Object[]{double.Parse(param[0]), double.Parse(param[1])};
}
}