none
[UWP]Question about x:bind syntax... RRS feed

  • Question

  • <ListView.ItemTemplate>
                        <DataTemplate x:DataType="x:String" x:DefaultBindMode="TwoWay">
                            <RelativePanel Width="376" Height="60" Padding="0" Margin="0">
                                <Button Click="Button_Delete_Click" Width="40" Height="40" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True">
                                    <SymbolIcon Symbol="Delete"/>
                                </Button>
                                <StackPanel Orientation="Vertical">
                                    <Border Height="30">
                                        <TextBlock Text="{x:Bind IO:Path.GetFileName(?), Mode=OneWay}" VerticalAlignment="Center"/>
                                    </Border>
                                    <Border Height="30">
                                        <TextBlock Text="{x:Bind}" VerticalAlignment="Center"/>
                                    </Border>
                                </StackPanel>
                            </RelativePanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>

    how can i set parameter in this case? ( => IO:Path.GetFileName(?) )

    Is there exist equivalent keyword like 'this'?


    Sunday, April 14, 2019 2:49 PM

All replies

  • Hi,

    If you need to set parameter in the function of the x:bind, you need to make sure the parameter is also contained in the data context as a property. Then you could just put the parameter into in ().

    For example, the following code shows how to put the color in the the ColorEntry.Brushify() function.

     <DataTemplate x:DataType="local:ColorEntry">
        <Grid Background="{x:Bind local:ColorEntry.Brushify(Color), Mode=OneWay}" Width="240">
            <TextBlock Text="{x:Bind ColorName}" Foreground="{x:Bind TextColor(Color)}" Margin="10,5" />
        </Grid>
    </DataTemplate>
    class ColorEntry
    {
        public string ColorName { get; set; }
        public Color Color { get; set; }
    
        public static SolidColorBrush Brushify(Color c)
        {
            return new SolidColorBrush(c);
        }
    
        public SolidColorBrush TextColor(Color c)
        {
            return new SolidColorBrush(((c.R * 0.299 + c.G * 0.587 + c.B * 0.114) > 150) ? Colors.Black : Colors.White);
        }
    }
     

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 7:31 AM
    Moderator
  • Thank you, but I already knew about that.

    I just don't want make new class for DataContext.

    My Listview Bound to ObservableCollection<string>.

    I want to know how to input parameter for method using x:string.

    Monday, April 15, 2019 11:06 AM
  • Hello,

    Specifying "ToString", you may be able to make it work.

        <DataTemplate x:DataType="x:String">
            <Border>
                <TextBlock Text="{x:Bind IO:Path.GetFileName(ToString), Mode=OneWay}"/>
            </Border>
        </DataTemplate>

    Monday, April 15, 2019 1:37 PM
  • FEC-4RP/

    that makes a error...

    error CS0103: The name 'TryGet_M_ToString' does not exist in the current context


    Monday, April 15, 2019 2:03 PM
  • Oh so ... In VS2015, it was possible to let XAML compiler yield right code by giving ToString there. But such a trick may be no longer valid. Then, I have no idea. ... It might be better to use IValueConverter.
    Monday, April 15, 2019 3:00 PM