I have a StatusBarItem
which I want to display the following message at any given moment "X/Y" where X is the number of the row of the currently selected element, and Y is the row count.
Now, if I use the Content="{Binding ElementName=lvTabela, Path=SelectedIndex}"
code in xaml I am able to get the first attribute to display, but I'm not sure how I can get both.
I suppose I could always use two StatusBarItem
elements next to each other, but I'd like to learn how to do this as well.
Oh, and while we're at it, how would I go about incrementing the selected index? Basically, instead of -1 to rowCount-1 I'd like it to display 0 to rowCount. I've seen people using the formatter to add additional text to their data binding, but I'm not sure how I can manipulate the data like that.
You got 2 options:
Either set the Content
of the StatusbarItem
a TextBlock
to use StringFormat
with MultiBinding
, Something like:
<StatusBarItem>
<StatusBarItem.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}/{1}">
<MultiBinding.Bindings>
<Binding ElementName="listView"
Path="SelectedIndex" />
<Binding ElementName="listView"
Path="Items.Count" />
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StatusBarItem.Content>
</StatusBarItem>
or use a Converter on the MultiBinding
to not have to use the TextBlock
<Window.Resources>
<local:InfoConverter x:Key="InfoConverter" />
</Window.Resources>
...
<StatusBarItem>
<StatusBarItem.Content>
<MultiBinding Converter="{StaticResource InfoConverter}">
<MultiBinding.Bindings>
<Binding ElementName="listView"
Path="SelectedIndex" />
<Binding ElementName="listView"
Path="Items.Count" />
</MultiBinding.Bindings>
</MultiBinding>
</StatusBarItem.Content>
</StatusBarItem>
and InfoConverter.cs:
class InfoConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
return values[0].ToString() + "/" + values[1].ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
StatusBarItem
expects an object, when StringFormat
returns a string and hence why we cannot use StringFormat
with the MultiBinding
without the TextBlock
which can take a string in it's Text
field.
As for your second question on how to increment the SelectedIndex
value, you can do that with the Converter easily,
Just switch the Convert(...)
function in InfoConverter.cs
to
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
return (System.Convert.ToInt32(values[0]) + 1).ToString() + "/" + values[1].ToString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.